-1

http://ideone.com/T5wSRV this is the link to below code

SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));

//Time in IST
Date date=dateFormatIST.parse( dateFormatIST.format(new Date()) );
System.out.println(date);

this is not giving correct IST time where as code below is working fine . why? http://ideone.com/9KSaZx this is the link to below code which is giving the desired output.Help me understand the behavior.

SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));

//Local time zone   
SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

//Time in IST
Date date=dateFormatLocal.parse( dateFormatIST.format(new Date()) );
System.out.println(date);
  • Please give the expected and actual results in each case. And why are you formatting, then parsing, then formatting again (using Date.toString) in the latter case? And what is your time zone? Your question is currently very unclear. (It's also unhelpful to have a variable called `dateFormatGmt` which is using IST...) – Jon Skeet Mar 16 '15 at 15:34
  • This gives me correct time. What did you expect as result, at current IST time? – Rohit Jain Mar 16 '15 at 15:34
  • 1
    it seems that IST is not a "deprecated" time zone and should be replace, a beginning of answer here : http://stackoverflow.com/questions/11264697/java-timezone-strange-behavior-with-ist – vincent Mar 16 '15 at 15:37
  • @RohitJain i have added the output link. – Abhishek Prasad Mar 16 '15 at 15:48
  • @JonSkeet I am sorry.I have edited the question. – Abhishek Prasad Mar 16 '15 at 15:48
  • But you still haven't told us your local time zone, or the expected output, or the actual output. (And ideally, don't just include links - include the short but complete program, with expected/actual output, directly in the question.) – Jon Skeet Mar 16 '15 at 15:50
  • @JonSkeet i want the date and time in IST. – Abhishek Prasad Mar 16 '15 at 15:52
  • 1
    That's not helpful. Showing the *exact output you're getting* and the exact output you're expecting would be helpful, along with answering my question about the time zone of your system. Don't add the answers to these questions as comments; edit your question to address *all* of the issues I've listed. – Jon Skeet Mar 16 '15 at 15:54
  • 3
    `Date` objects do not contain timezone information. If you print a `Date` object, for example with `System.out.println(date);`, it will always be displayed in the timezone that your system is set to. – Jesper Mar 16 '15 at 15:59
  • @Jesper Make that an answer :) – Rohit Jain Mar 16 '15 at 16:02
  • @Jesper I agree to that . But when I set the timezone to IST then after using format it converts date and time to IST but timezone remains of the system and it i expected.but the first code is not behaving in this way.why? – Abhishek Prasad Mar 16 '15 at 16:07

1 Answers1

3

The behaviour is logical. The point is that there is no information of time-zone is a Date object. A Date object contains Universal Time. And when you format then parse the formatted string, you still have the same date:

I commented the code with the results:

    SimpleDateFormat dateFormatIST = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    dateFormatIST.setTimeZone(TimeZone.getTimeZone("IST"));

    //Local time zone   
    SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

    //Time in IST
    Date d = new Date();
    System.out.println(d);
    // Mon Mar 16 16:57:19 CET 2015   

=> now in my TZ (CET)

    System.out.println(dateFormatIST.format(d));
    // 2015-Mar-16 21:27:19   

=> now in IST TZ

    System.out.println(dateFormatLocal.format(d));
    // 2015-Mar-16 16:57:19   

=> now in my TZ (CET)

    Date dateIST = dateFormatIST.parse(dateFormatIST.format(d));
    System.out.println(dateIST);
    // Mon Mar 16 16:57:19 CET 2015 

=> The dateIST object contains still "now", and the format is default local which is CET

    Date dateLoc = dateFormatLocal.parse(dateFormatLocal.format(d));
    System.out.println(dateLoc);
    // Mon Mar 16 16:57:19 CET 2015 

=> same thing as above

    Date dateLocIST = dateFormatLocal.parse(dateFormatIST.format(d));
    System.out.println(dateLocIST);
    // Mon Mar 16 21:27:19 CET 2015   

=> dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015".

If you need to translate dates between different time-zone, you certainly need to go for the Calendar class.

  • Thanks for the answer.But can you please elaborate your last 2 lines especially the last interpret part "dateFormatIST.format(d) gives "2015-Mar-16 21:27:19", and dateFormatLocal.parse() will interpret it like a local (CET for me) date. The result is then "Mon Mar 16 21:27:19 CET 2015"" – Abhishek Prasad Mar 16 '15 at 16:38
  • As I said, `dateFormatLocal.parse("2015-Mar-16 21:27:19")` will give a `Date` object that correspond to the `String` "2015-Mar-16 21:27:19" in CET local time. It's then obvious that when you just print it with the default local which CET (for me), you obtain the same date/time, but with a different format. –  Mar 16 '15 at 17:16
  • If you're using Java 8, then have a look at the new date and time API in the package `java.time`, which is much better than the old `java.util.Date` and `java.util.Calendar` classes. – Jesper Mar 17 '15 at 08:33