简体   繁体   中英

Converting date to EST in java 8

I'm trying to convert date to following timezone but result is not as expected - The requirement i got is saying for example conversion from PMST to EST the output should be 2 hour less.

PMST, NST, AST, EST, CST, MST, PST, AKST, HAST

String inputDate = "2017/04/30 08:10";
DateTimeFormatter sourceFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
LocalDateTime local = LocalDateTime.parse(inputDate, sourceFormatter);
ZonedDateTime zoned = local.atZone(TimeZone.getTimeZone("PMST").toZoneId());
ZonedDateTime requiredZone = zoned.withZoneSameInstant(TimeZone.getTimeZone("EST").toZoneId());
System.out.println(requiredZone);

Output- 2017-04-30T03:10-05:00

So you know that your input "local date" is in PMST timezone.

String localDateTimeString = "2017/04/30 08:10";

But you should be able to notice that the above does not carry the timezone information which you know. So you need to add that information to the String.

But what we have now is just a String , so first we have to decide on the date time format which we will be using and it has to be TimeZone aware,

DateTimeFormatter dateTimeWithZoneFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm Z");

Here, Z represents the TimeZone offset. We know that PMST is UTC-0300 , so we need to add this information to our local time string,

String dateTimeString = "2017/04/30 08:10" + " -0300";

Now, we can read it using our date time formatter,

ZonedDateTime dateTimeInPMST = ZonedDateTime.parse(dateTimeString, dateTimeWithZoneFormatter);

And, now we can obtain the date in any time zone we want,

ZonedDateTime dateTimeInEST = dateTimeInPMST.withZoneSameInstant(TimeZone.getTimeZone("EST").toZoneId());

Edit 1 ::

To get the offset for a timezone (lets say EST )

int offsetInSeconds = TimeZone.getTimeZone("EST").getRawOffset() / 1000;

ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(offsetInSeconds);

String zoneOffsetString = zoneOffset.toString();
// "-05:30"

Note this : in -05:30 , we will have to change our DateTimeFormat to suit this,

DateTimeFormatter dateTimeWithZoneFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm ZZZ");

Now, you can add this to any dateTimeString,

public String makeOffsetAwareDateTimeString(String dateTimeString, String timezone) {
    int offsetInSeconds = TimeZone.getTimeZone(timezone).getRawOffset() / 1000;
    ZoneOffset zoneOffset = ZoneOffset.ofTotalSeconds(offsetInSeconds);
    String zoneOffsetString = zoneOffset.toString();
    return dateTimeString + " " + zoneOffsetString;
}

This works for me. Modify for which ever zone is needed.

public class ZonedDateTime {

    private static final String DATE_FORMAT = "dd/M/yyyy hh:mm:ss a";

    public static void main(String[] args) {

        String dateInString = "26/12/2017 10:15:55 AM";
        LocalDateTime ldt = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE_FORMAT));

        ZoneId newYokZoneId = ZoneId.of("America/New_York");
        ZonedDateTime nyDateTime =ldt.atZone(newYokZoneId);
        System.out.println("Date (New York) : " + nyDateTime);

        DateTimeFormatter format = DateTimeFormatter.ofPattern(DATE_FORMAT);
        System.out.println("Date (New York) : " + format.format(nyDateTime));

    }

}

Find all the zones using this.

TreeSet<String> sortedZones = new TreeSet<>(ZoneId.getAvailableZoneIds());

    for (String zone : sortedZones) {
            System.out.println(zone);
    }

Avoid pseudo-zones

Never use the 3-4 letter abbreviations often seen in the media, such as CST , IST , and EST . These are not true time zones , are not standardized, and are not even unique(!).

True time zones: continent/region

Instead, determine your true time zone intended. Time zones have names in format of continent/region such as America/Montreal and Africa/Casablanca and Pacific/Auckland .

Alter your input to comply with ISO 8601 standard formats.

String input = "2017/04/30 08:10".replace( " " , "T" ) ;

Parse as a LocalDateTime as your input lacks an indicator of offset-from-UTC or time zone.

LocalDateTime ldt = LocalDateTime.parse( input ) ;

Apply a time zone if you are certain it was intended for this input.

ZoneId z = ZoneId.of( "America/New_York" ) ;
ZonedDateTime zdt = ldt.withZoneSameInstant( z ) ;

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