0

I have the following codes. The string form of the java date time is gmt+0. Thus I need to later convert it according to different local timezone but when I try to set the default timezone it keep going back another 8 hours cause my machine is on gmt+8.The output is showing me 2017-12-09 09:00:00 but I want to remain as 2017-12-09 17:00:00 because this is gmt+0.

    String existingTime = "2017-12-09 17:00:00";
    String newTime = "2017-12-09 14:00:00";

    Date existingDateTime = null;
    Date newDateTime = null;
    Date localexistingDateTime = null;
    Date localnewDateTime = null;
    DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    TimeZone tz = TimeZone.getDefault();
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    // sdf.setTimeZone(TimeZone.getTimeZone("GMT+8:30"));

    try {
        existingDateTime = dateTimeFormat.parse(existingTime);
        newDateTime = dateTimeFormat.parse(newTime);

        System.out.println("GMT existingDateTime" + sdf.format(existingDateTime));
        System.out.println("GMT newDateTime" + sdf.format(newDateTime));
    } catch (ParseException ex) {
        System.out.println("MyError:Parse Error has been caught for date parse close");
        ex.printStackTrace(System.out);
    }
vinS
  • 1,417
  • 5
  • 24
  • 37
user8012596
  • 693
  • 1
  • 8
  • 16
  • 2
    I cannot more strongly advise against using the legacy `java.util.Date`, this being one of the reasons. You should instead look for the appropriate classes in the `java.time` package that is appropriate for your use case. – Joe C Dec 09 '17 at 13:58
  • @JoeC so what is your suggestion to overcome this cause most of the solution on google still showing this. – user8012596 Dec 09 '17 at 14:00
  • 2
    My suggestion is to stop using `java.util.Date` and use the classes in the `java.time` package instead. – Joe C Dec 09 '17 at 14:00
  • @JoeC will do accordingly like you suggested actually what is the key reason for it does it have some bugs in it ? – user8012596 Dec 09 '17 at 14:02
  • 1
    I refer you to [this question](https://stackoverflow.com/questions/28730136/should-i-use-java-util-date-or-switch-to-java-time-localdate). – Joe C Dec 09 '17 at 14:05
  • Ok I got it now I am switching to java.time.*; Any good code snippet for my solution. – user8012596 Dec 09 '17 at 14:10
  • 1
    There are already a number of questions and answers on conversion between time zones. Did you take a look? I recommend doing so. I also agree with Joe C, so particularly look for the answers using `java.time`, there are some. – Ole V.V. Dec 09 '17 at 14:12
  • 1
    @OleV.V. ok I will get some answer if not I will put a new question thank you. – user8012596 Dec 09 '17 at 14:15

1 Answers1

3

This snippet may get you started using java.time, the modern Java date and time API. It is also known as JSR-310 after the Java Specification Request that first described it.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    String existingTime = "2017-12-09 17:00:00";
    OffsetDateTime existingDateTime = LocalDateTime.parse(existingTime, formatter)
            .atOffset(ZoneOffset.UTC);
    System.out.println("UTC existingDateTime: " + existingDateTime.format(formatter));
    System.out.println("Pyongyang existingDateTime: " 
            + existingDateTime.atZoneSameInstant(ZoneId.of("Asia/Pyongyang"))
                    .format(formatter));
    System.out.println("Singapore existingDateTime: " 
            + existingDateTime.atZoneSameInstant(ZoneId.of("Asia/Singapore"))
                    .format(formatter));

The output from running the snippet is:

UTC existingDateTime: 2017-12-09 17:00:00
Pyongyang existingDateTime: 2017-12-10 01:30:00
Singapore existingDateTime: 2017-12-10 01:00:00

This was the output I got on my computer (in Europe/Berlin time zone), but it’s easier to keep the modern classes independent of the JVM’s time zone, so you should get the same output on your computer.

EDIT: You asked in a comment if it isn’t possible to use an offset of GMT+09:00 instead of a time zone. I’m unsure why you will want to do that since real people live in time zones rather than in offsets, but it’s easy when you know how:

    System.out.println("GMT+09:00 existingDateTime: "
            + existingDateTime.withOffsetSameInstant(ZoneOffset.ofHours(9))
                    .format(formatter));

Output:

GMT+09:00 existingDateTime: 2017-12-10 02:00:00

A LocalDateTime is a date and time without time zone or offset information. Since your string doesn’t contain offset or zone, I use this for parsing. And since you told me your date-time was in GMT+0, I convert it to an OffsetDateTime with offset UTC first thing. From there it’s straightforward to convert it into different local time zones. So I demonstrate that for a couple of time zones, each time formatting the date-time using the same formatter I used for parsing, since I gather you tend to like the yyyy-MM-dd HH:mm:ss format.

I always give time zone in the region/city format. This is unambiguous (contrary to three letter abbreviations; for example, PYT may mean Paraguay Time or Pyongyang Time). And it will automatically take care of summer time (DST) in case the time zone uses such. Even historic changes in zone offset are built-in.

To learn to use java.time, see the Oracle Tutorial on Date Time and search for relevant questions on Stack Overflow, always looking for the java.time answers (there’s a wealth of old answers using the outdated classes, skip those). Even more places on the net hold valuable resources. Your search engine and your ability to distinguish good from poor are your friends.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • v.v thank you for the snippet. How about me using e.g. gmt+09:00 as a replacement to zoneid is possible right? – user8012596 Dec 09 '17 at 14:56
  • 1
    thank you. The reason is that in my db I kept it both as GMT-05:00 and also -05:00. So what best should I use ZoneOffset.ofHours(9). Can I use it as ZoneOffset.ofHours(-05:00) – user8012596 Dec 09 '17 at 15:07