how to get abbreviated timezone?

2019-05-26 06:24发布

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.

1条回答
Deceive 欺骗
2楼-- · 2019-05-26 06:40

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!

查看更多
登录 后发表回答