0

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)"

AleSod
  • 422
  • 3
  • 6

3 Answers3

0
return new SimpleDateFormat("MMM dd, yyyy HH:mm:ss z (zZ)").format(new Date()));

From SimpleDateFormat JavaDoc:

  • z: General Time Zone (Pacific Standard Time; PST)
  • Z: RFC 822 time zone (-0800)
Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25
0

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));
}
Viet
  • 3,349
  • 1
  • 16
  • 35
  • It is ok for my question but if I want to use 'GMT' in braces then what should I use in place of 'z'. eg. "OCT 18, 2016 16:04:04 HST (GMT-10:00)" – Gaurav Sharma Nov 29 '16 at 08:50
  • yes, you should try it yourself, you can add any character eliminate SimpleDateFormat letters http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html – Viet Nov 29 '16 at 08:55
0
DateFormat dateFormat =new SimpleDateFormat("MMM dd, yyyy HH:mm:ss z (zZ)");
return dateFormat.format(new Date(date));

For more information about date formats refer this

Venu
  • 348
  • 4
  • 17
  • These troublesome classes are now legacy, supplanted by the java.time classes. And you ignore the crucial issue of time zone. – Basil Bourque Dec 02 '16 at 06:27