Gaurav Sharma Gaurav Sharma - 10 days ago 4
Java Question

How to get formatted date with date-time and timezone with offset in java

I want to get formatted date and time with timezone and offset. recently I used this code for formatting.

public static String getFormattedLicenseDate(long date) {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
return dateFormat.format(new Date(date));
}


If the input is 1476786856310 the this method returns "10/18/16 4:04:16 PM HST" but I want to modify this method to get "OCT 18, 2016 16:04:04 HST (HST-10:00)"

Is there any pattern to get this output like "OCT 18, 2016 16:04:04 HST (HST-10:00)"

Answer

You can use SimpleDateFormat with format "MMM dd, yyyy HH:mm:ss z (zXXX)"

public static String getFormattedLicenseDate(long date) {
    SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss z (zXXX)");
    return format.format(new Date(date));
}