1

My system located in Singapore (timezone - "Asia/Singapore") receives a String datetime (yyyy-MM-dd HH:mm:ss) from a external system in Indonesia (timezone - "Asia/Jarkata").

How do I convert the received String datetime to UTC in java 1.7?

This is my code:

public void convertToUtc() {
    String inputTime = "2018-02-02 10:09:00";
    TimeZone inputTz = TimeZone.getTimeZone("Asia/Jarkarta");
    TimeZone utcTz = TimeZone.getTimeZone("UTC");

    SimpleDateFormat inputSdf = new SimpleDateFormat(DateTimeUtils.DATE_TIME_FORMAT);
    inputSdf.setTimeZone(inputTz);

    SimpleDateFormat utcSdf = new SimpleDateFormat(DateTimeUtils.ISO_DATE_TIME_FORMAT);
    utcSdf.setTimeZone(utcTz);

    // From time
    Date fromDate = null;
    try {
        fromDate = inputSdf.parse(inputTime);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Convert to UTC
    String sUtcDateTime = utcSdf.format(fromDate);
    System.out.println("UTC: " + sUtcDateTime); // UTC: 2018-02-02T10:09:00.000Z. Expected 2018-02-02T03:09:00.000Z
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
chrizonline
  • 4,779
  • 17
  • 62
  • 102

2 Answers2

2

janith1024’s answer is correct. However, I believe the real problem is that the old (and long outdated) TimeZone class behaves badly and doesn’t inform you of your spelling mistake. After all, we could all make that. So the real solution is to use java.time, the modern Java date and time API. On Java 7 (and 6) add ThreeTen Backport to your project, import:

import org.threeten.bp.ZoneId;

And then:

        ZoneId inputTz = ZoneId.of("Asia/Jarkarta");

This gives you a org.threeten.bp.zone.ZoneRulesException: Unknown time-zone ID: Asia/Jarkarta. I should say that this greatly increases your chance of discovering your spelling error (the correct spelling is Asia/Jakarta).

The documentation of TimeZone.getTimeZone() says that it returns

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

However I am posting this answer because your issue is not just a lone example. Over and over we see questions on Stack Overflow coming from the old date and time classes showing surprising behaviour and in particular not detecting problems with the data we pass to them that it’s easy to detect. I very warmly recommend using the modern API instead.

In Java 8 and later, java.time is built-in, and you should import your date and time classes from java.time with subpackages rather than from org.threeten.bp.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

I check your code there was a spelling mistake in time zone so I correct it

  public static void convertToUtc() {
    String inputTime = "2018-02-02 10:09:00";
    TimeZone inputTz = TimeZone.getTimeZone("Asia/Jakarta");
    TimeZone utcTz = TimeZone.getTimeZone("UTC");

    SimpleDateFormat inputSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    inputSdf.setTimeZone(inputTz);

    SimpleDateFormat utcSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    utcSdf.setTimeZone(utcTz);

    // From time
    Date fromDate = null;
    try {
      fromDate = inputSdf.parse(inputTime);
    }
    catch (ParseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    // Convert to UTC
    String sUtcDateTime = utcSdf.format(fromDate);
    System.out.println("UTC: " + sUtcDateTime); // print this UTC: 2018-02-02 03:09:00. 
  }
janith1024
  • 1,042
  • 3
  • 12
  • 25
  • Troublesome old `TimeZone` class. Pretending all is well and giving you GMT instead of the time zone you wanted. If you could only use java.time and it `ZoneId` class instead. Hey, you can!! Look into [ThreeTen Backport](http://www.threeten.org/threetenbp/), the backport of java.time to Java 6 and 7. – Ole V.V. Mar 03 '18 at 14:25