0

I am trying to convert date string to UTC format from another time zone in Java. we have only time zone offset like "-06:00". Can any one help me how to convert the date time to UTC format using time zone offset.

Thanks

This for java version 1.7 . I have tried with following snippet but receiving the same input as output.

String dateInString = "02/04/2019 18:17:15";                
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = formatter.parse(dateInString);          
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");             
dateFormat.setTimeZone(TimeZone.getTimeZone("+5:30"));
String dateStr = dateFormat.format(date);
System.out.println(dateStr);

Output 02/04/2019 18:17:15

hc_dev
  • 8,389
  • 1
  • 26
  • 38
KNow
  • 163
  • 2
  • 12
  • 2
    You should definitely use the new Date and Time API, available in the `java.time` package. `Date` is obsolete. – MC Emperor Feb 04 '19 at 18:48
  • @MCEmperor Question says "This for java version 1.7", so `Date` is obsolete, and there is no `java.time` package. Even with the backport there is no `java.time` package, since that is all in package `org.threeten.bp`. – Andreas Feb 04 '19 at 19:20
  • @MadProgrammer Not a duplicate. The issue is the invalid time zone id. See [my answer](https://stackoverflow.com/a/54522691/5221149). – Andreas Feb 04 '19 at 19:20
  • 1
    @Andreas Or threeten backport or Joda Time. [It's not hard to find](https://www.google.com/search?q=alternative+to+"java.time"+7) alternatives for Java below 8. But you're right, I should have mentioned that in the first place. – MC Emperor Feb 04 '19 at 19:25
  • `dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss");` `LocalDateTime.now(ZoneId.of("Z")).format(dateFormatter);`worked for me from having used `SimpleDateFormat` for UTC/Zulu/GMT timezone. This was derived from the link provided by @MadProgrammer – DarceVader Feb 04 '19 at 19:26
  • @DarceVader Question says "This for java version 1.7", so that doesn't work. Besides, how is that a solution for converting time to offset `+5:30` *specifically*, when your code snippet doesn't mention that offset? – Andreas Feb 04 '19 at 20:09
  • @Andreas You're right about 1.7, but OP did ask about converting to UTC. Also why I posted as a comment rather than a solution, as I was formatting the date in a similar fashion as OP until I saw the link shared by MadProgrammer. – DarceVader Feb 04 '19 at 20:30
  • Please allow me to emphasize even though it’s already been said: The good solution is to add [ThreeTen Backport](https://www.threeten.org/threetenbp/) to your project and use the modern `ZoneId` class and the rest of java.time, the modern Java date and time API. `ZoneId.of` will throw an exception making you aware that `+5:30` is not a valid zone ID, which will give you the information you need for avoiding your issue. You’ve hit just one of the design problems in the old classes, there’s an uncountable lot of them. – Ole V.V. Feb 06 '19 at 13:49
  • Rather than an offset, for most purposes it’s better to use a real time zone, for example Asia/Colombo or Asia/Kolkata. – Ole V.V. Feb 06 '19 at 13:51

1 Answers1

0

Unfortunately, TimeZone.getTimeZone(String ID) returns:

the specified TimeZone, or the GMT zone if the given ID cannot be understood.

The "+5:30" time zone cannot be understood, so you get GMT.

Change to "GMT+5:30" will make your code work, i.e. it'll print:

02/04/2019 23:47:15

See the javadoc of TimeZone for valid ID syntax:

The syntax of a custom time zone ID is:

CustomID:
        GMT Sign Hours : Minutes
        GMT Sign Hours Minutes
        GMT Sign Hours
Sign: one of
        + -
Hours:
        Digit
        Digit Digit
Minutes:
        Digit Digit
Digit: one of
        0 1 2 3 4 5 6 7 8 9

As you can see, it must always start with GMT.

Andreas
  • 154,647
  • 11
  • 152
  • 247