Category Archives: Java

Convert Java stacktrace to String

stacktrace
To present java stacktrace as string use next snippet:

Exception e = new Exception("EX!");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);

String stacktraceAsString = sw.toString();

Thanx to http://stackoverflow.com/a/1149712

JSF convertDateTime timeZone error fix

To show corrent date JSF with convertDateTime you can use next:

<context-param> 
  <param-name>
    javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE
  </param-name> 
  <param-value>
    true
  </param-value> 
</context-param>

So, in page:

<h:outputText value="#{Bean.date}">
  <f:convertDateTime pattern="HH:mm:ss"/>
</h:outputText>

Thanx to http://stackoverflow.com/a/7491706 !

Could not transfer artifact with Maven

mvn
You may see the error when try to make maven project:
Could not transfer artifact Connection refused: connect

The problem may be with your internet connection and can be fixed by adding the following settings in maven settings.xml file (for windows: %USERPROFILE%\.m2 folder):


        
            true
            http
            your_proxy_host
            your_proxy_port
            
        
    

Thanx to http://stackoverflow.com/a/16729906.

Trim string in Java

char
Don’t like string frameworks? Use POJO java!

static String trim(String value, char character) {
    if (value == null || value.isEmpty()) {
        return value;
    }
    String normalized = value.trim();

    int startIndex = 0;
    while (normalized.charAt(startIndex) == character) {
        startIndex++;
    }

    int endIndex = value.length() - 1;
    while (normalized.charAt(endIndex) == character) {
        endIndex--;
    }

    return normalized.substring(startIndex, endIndex + 1).trim();
}

Simple!

Why JavaEE ?

java_ee
Why JavaEE is so popular? Not because it is the super useful, just this is implementation of all incoming business requirements for a time. This is so called template patterns for business, nothing more: not technical brilliance, not ideological concern.

Here is high availability, high scalability, high reliability platform for building business infrastructure.
The time from first release is a proof.
Use java!

Floating point operations in XPath

xpath
You may need to perform arithmetic operation with XPath-expression like sum, etc. But Saxon parser cast all floating point values to DOUBLE by default (as Java).
If you have next xml:

<root>
    <value>1.555</value>
    <value>1</value>
    <value>-1</value>
</root>

So, for sum you use next XPath expression:

sum(/root/value)

And in result you’ll have NOT 1.555, but 1.4999999754 for example.
This is normal java behavior! Don’t panic!
Just use some trick:

sum(/root/value/xs:decimal(.))

And it gives you 1.555 in result.

As you see, adding /xs:decimal(.) solves the problem with data losses.

Capitalize word algorithm in Java

capit
Here is simple snippet to capitalize string:

private static String capitalize(String value) {
    return value == null ? null : 
        value.substring(0, 1).toUpperCase() + value.substring(1);
}

Simple!

Thanx to
http://stackoverflow.com/questions/5725892/how-to-capitalize-the-first-letter-of-word-in-a-string-using-java