简体   繁体   中英

Java set timezone does not default to gmt+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);
    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM