Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

datetime - Java How to get time printed in IST

I am able to get the time as well as timezone currently. but it always printed in

Wed May 23 11:01:08 GMT+05:30 2012

As TimeZone i am getting GMT+05:30. instead of this i want to print IST

I tried

 SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");

but still i am getting GMT+05:30

TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT); is also givine me GMT+05:30

There are various things i tried but did not get desire ouput. I have used Calender but still unable to print IST .

Can anyone please let me know how to print IST in Time (even i dont want to print Indian standard time)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You haven't shown where you're actually using the SimpleDateFormat. Here's a short but complete example which does show IST:

import java.text.*;
import java.util.*;

class Test {
    public static void main(String[] args) {
        SimpleDateFormat sd = new SimpleDateFormat(
            "yyyy.MM.dd G 'at' HH:mm:ss z");
        Date date = new Date();
        // TODO: Avoid using the abbreviations when fetching time zones.
        // Use the full Olson zone ID instead.
        sd.setTimeZone(TimeZone.getTimeZone("IST"));
        System.out.println(sd.format(date));
   }
}

Output on my machine:

2012.05.23 AD at 11:18:08 IST

Note that if TimeZone.getDefault() isn't showing "IST" then the issue may be:

  • That the system can't actually identify your system time zone
  • That your Java installation doesn't have full time zone information regarding i18n

Which version of Java are you using, and on what platform?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...