1

I'm trying to convert system local date to UTC. Below is my code and it looks working for MST and EST formats. But, it is not working as expected.

        String inputDate = "Wed Apr 13 04:00:00 IST 2022";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        Date date = sdf.parse(inputDate);
        DateFormat formatUTC = new SimpleDateFormat("MM/dd/yyyy");
        formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
        String result = formatUTC.format(date); 
        System.out.print(result); // 04/13/2022

I see that IST zone is 5hrs 30mins ahead from the UTC universal time. So, I should get 04/12/2022 for the given input. But, getting 04/13/2022. what am I doing wrong here? Please advise.

Learner
  • 31
  • 3
  • 1
    Print out the complete date ***and time*** to see what is happening. – Jim Garrison Apr 13 '22 at 01:58
  • 3
    `SimpleDateFormat`, oh these classes need to go and die - seriously though, use the `java.time` APIs, save your sanity – MadProgrammer Apr 13 '22 at 02:26
  • Learner I can understand how some programmers who learned Java in the last millennium have learned `SimpleDateFormat` and `Date`. If *Learner* implies you are learning Java after 2014, you are learning the wrong thing. You need to learn [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html) already mentioned by @MadProgrammer. – Ole V.V. Apr 13 '22 at 03:49
  • *I see that IST zone is 5hrs 30mins ahead from the UTC universal time.* It depends on whether you understand IST as Irish Summer Time, Israel Standard Time, India Standard Time or something else. How your `SimpleDateFormat` understood it I cannot tell. Three letter time zone abbreviations are generally ambiguous. Avoid relying om them. MST and EST are ambiguous too, so I cannot tell how those were interpreted either. – Ole V.V. Apr 13 '22 at 03:53
  • The solution to your unexpected result is in my answer here: [how convert date" formate including IST](https://stackoverflow.com/a/66516212/5772882). And also [here](https://stackoverflow.com/questions/57733849/ist-mapped-to-wrong-zoneid-in-java-time-library). Do they answer your question? – Ole V.V. Apr 13 '22 at 04:05
  • 1
    A close vote? This question already contains desired behaviour (`04/12/2022`), a specific problem (undesired output `04/13/2022`) and the shortest code to reproduce. There’s nothing missing. It additionally shows research effort (tried with MST and EST). It’s a good question. If to close, then as a duplicate, not because of missing debugging details because it isn’t missing any. – Ole V.V. Apr 13 '22 at 04:32

1 Answers1

0

Try setting the timezone for inputDate as well. Try the below code:

        String inputDate = "Wed Apr 13 04:00:00 IST 2022";
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
        Date date = sdf.parse(inputDate);
        DateFormat formatUTC = new SimpleDateFormat("MM/dd/yyyy");
        formatUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
        String result = formatUTC.format(date);
        System.out.print(result);

Take a look at this answer.

Prog_G
  • 1,539
  • 1
  • 8
  • 22