0

After several trial and errors, posting this for a hope to getrid of the issue with getting Todays time in JST(Japan).

Here is my first approach using Date and SimpleDateFormat:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("Japan"));
        Date date = new Date();
        System.out.println(date);                       // Mon Dec 08 15:29:25 IST 2014
        System.out.println(dateFormat.format(date));    //12/08/2014 18:59:25
        System.out.println(dateFormat.parse(dateFormat.format(date)));  //Mon Dec 08 15:29:25 IST 2014

Here I am getting right one with dateFormat.format(date). Howerver it returns in IST when parsed to date again(dateFormat.parse(dateFormat.format(date))).

My second approach using Calendar class:

Calendar cal = Calendar.getInstance();
        cal.setTimeZone(TimeZone.getTimeZone("Japan"));
        System.out.println(cal.getTime());  //Mon Dec 08 15:31:44 IST 2014
        System.out.println(new Date());     //Mon Dec 08 15:31:44 IST 2014

My goal is to set Todays date in Japan time.

janasainik
  • 811
  • 5
  • 20
  • 40
  • 2
    Have you looked at joda-time? There's an answer here that will help: http://stackoverflow.com/questions/19002978/in-joda-time-how-to-convert-time-zone-without-changing-time - it's an excellent replacement for all calendar/dates manipulation in java – Mark Fisher Dec 08 '14 at 10:10

2 Answers2

2

Try the following with joda-time's LocalDateTime

LocalDateTime localDateTime = new LocalDateTime(DateTimeZone.forID("Asia/Tokyo"));
System.out.println(localDateTime.toString());
Mark Fisher
  • 9,838
  • 3
  • 32
  • 38
  • Wont there be an alternative without using JODA library? I mean with java JDK itself? – janasainik Dec 08 '14 at 10:33
  • Sure, but after having seen unit tests fail at the start of this month because people make index mistakes with Calendar, believe me that Joda is a much better library to use and easier to reason. It does in 1 line what JDK does in several, and with less fragility. If you can't use an additional library like this, then I hope someone else can work through the JDK version for you. – Mark Fisher Dec 08 '14 at 11:05
0

I have achieved it by creating one more instance for SimpleDateFormat and parsing through it like this.

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("Japan"));
        System.out.println(dateFormat.parse(dateFormat.format(new Date())));    //Mon Dec 08 17:04:01 IST 2014
        System.out.println(dateFormat2.parse(dateFormat.format(new Date())));   //Mon Dec 08 20:34:01 IST 2014

If I use dateFormat object to parse, its displaying in IST. Where as if I use dateFormat2 then it returns the date in JST format.

janasainik
  • 811
  • 5
  • 20
  • 40
  • I don't see any difference in out put, also I would like to emphasize that the behavior is as per expectation. As you are creating date object in your local machine its being created in IST as Date Object is dependent on local machine time zone. You can check it by changing your machine timezone to Japan timezone and run the application again. However you can have date formatted in different time zone for view purpose rather than bothering about the underlying date object. Can you please describe in detail what are you trying to achieve exactly ? – Sariq Shaikh Dec 08 '14 at 11:46
  • My purpose is to get the time in JST even though the server running in different timezone. If i dont use another SimpleDateFormat object, it returns date as per server timezone which I don't want. You can watchout the last line of the source which shows JST time. – janasainik Dec 09 '14 at 11:19
  • I guess you want to display the time in JST format and that you are able to achieve by formatting the object with DateFormat.format method. Why you need to again parse the formatted string to have Date Object. In the last line again it gives time in IST which is as per expectation. – Sariq Shaikh Dec 09 '14 at 11:43
  • There is a time difference in last two lines! – janasainik Dec 09 '14 at 11:50