Thursday, 31 August 2017

How to convert Date and Time between Timezone in Java?

TimeZone represents a time zone offset, and also figures out daylight savings.

Typically, we can get a TimeZone using getDefault() method which creates a TimeZone based on the time zone where the application is running. 

For example, if the application is running in India, getDefault() method creates a TimeZone object based on Indian Standard Time (IST).

We can also get a TimeZone using getTimeZone along with a time zone ID. For instance, the time zone ID for the Indian Standard Time (IST) zone is Asia/Calcutta. So, we can get a IST TimeZone object with:

TimeZone timezone = TimeZone.getTimeZone("Asia/Calcutta");


Lets look into the code -


package com.anjan.timezone;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeZoneDemo {

public static String convertDateToTimeZone(Date date, TimeZone timeZoneID, String dateFmt) {

SimpleDateFormat sdf = new SimpleDateFormat(dateFmt);
sdf.setTimeZone(timeZoneID);

return sdf.format(date);

}

public static void main(String arg[]) {

Date date = new Date();

String dt = convertDateToTimeZone(date, TimeZone.getTimeZone("Asia/Calcutta"), "dd-MM-YYYY HH:mm:ss");

System.out.println("Date in Asia/Calcutta : "+dt);
dt = convertDateToTimeZone(date, TimeZone.getTimeZone("America/Los_Angeles"), "dd-MM-YYYY HH:mm:ss");
System.out.println("Date in America/Los_Angeles : "+dt);

}

}

Output -

Date in Asia/Calcutta : 31-08-2017 15:06:30

Date in America/Los_Angeles : 31-08-2017 02:36:30

Thursday, 17 August 2017

How to Convert StackTrace to String in Java

Stack Trace is a list of method calls from the point when the application was started to the point where the exception was thrown. 

Stack Trace is useful to debug the code and see from where exactly the Exception was originated and root cause of the problem. Stack trace of the exception can be printed to the standard error by calling the public void printStackTrace() method. Consider below given example.


Many times for debugging purposes, we'd like to convert the stack trace to a String so we can log it to our log file.
The following code shows how to do that -

package com.anjan.st;

import java.io.PrintWriter;
import java.io.StringWriter;

public class ConvertSTToString {

public static void main(String args[]) {
try {
int i = 10 / 0;
}catch(Exception e) {
System.out.println("Stack Trace ***\n");
e.printStackTrace();
}
try {
int i = 10 / 0;
}catch(Exception e) {
System.out.println("\nStack Trace as String **** \n"+convertToString(e));
}
}
/*
* This method will convert Stack Trace to String
*/
private static String convertToString(Throwable t) {
String strException = "";
StringWriter stringWriter = new StringWriter();
PrintWriter printWriternew PrintWriter(stringWriter);
t.printStackTrace(printWriter);
strException = stringWriter.toString();
return strException;
}
}

Output -

Stack Trace ***

java.lang.ArithmeticException: / by zero
at com.anjan.st.ConvertSTToString.main(ConvertSTToString.java:11)

Stack Trace as String **** 
java.lang.ArithmeticException: / by zero
at com.anjan.st.ConvertSTToString.main(ConvertSTToString.java:18)