2

I am trying to get an abbreviated time zone, ex. PST, EDT etc. I managed to get an offset but was unable to get an abbreviated timezone based on the offset.

Can anyone help?

For example, when

tz = America/Phoenix and time =1450759239340

DateTimeFormat.forPattern("zzz").withZone(tz).print(time);

I get -07:00 as a result.

Can I get an abbreviated time zone with these available code?

Thanks in advance.

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
Saurav Dangol
  • 894
  • 2
  • 10
  • 27
  • 1
    Unable to reproduce. On Window 7 with JDK 1.8.0_51 and joda-time-2.9.1, I get `MST` for 3 lower-case z's, and `Mountain Standard Time` for 4 z's. For 1, 2, and 3 uppercase Z's, I get: `-0700`, `-07:00`, and `America/Phoenix`. – Andreas Dec 22 '15 at 05:32
  • The same code above produces MST in java 1.7 but in 1.8.0-60, it is showing the offset. Could this be the changes in joda-time plugin to keep up with java 8? – Saurav Dangol Dec 22 '15 at 08:25

1 Answers1

1

Here is how , you can get different date format using SimpleDateFormat and Calendar class.

One of these is to get PDT, IST kind of output using z in small case.

if you want elaborated names use zzzz.

I would recommend choosing you desired date format from below:

"yyyy.MM.dd G 'at'              HH:mm:ss z" 2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"              Wed, Jul 4, '01
"h:mm a"                        12:08 PM
"hh 'o''clock' a, zzzz"         12 o'clock PM, Pacific Daylight Time
"K:mm a, z"                     0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"  02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"    Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"                 010704120856-0700        
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"    2001-07-04T12:08:56.235-0700

You may use SimpleDateFormat to get proper formatted string from date or vice-versa.

For example

String originalString = "2010-07-14 09:00:02";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(originalString);
String newString = new SimpleDateFormat("H:mm").format(date); 

See my blog on same.

Hope it helps!

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86