Parsing an ISO 8601 string local date-time as if in UTC

前端 未结 2 1919
清歌不尽
清歌不尽 2020-12-20 02:15

In java I need to make a Calendar object from a String in the format:

yyyy-MM-dd\'T\'HH:mm:ss

This string will always be set as GMT time. S

相关标签:
2条回答
  • 2020-12-20 02:57

    ISO 8601

    Your string format happens to comply with the ISO 8601 standard.

    If you are certain the string of the date-time value is meant for UTC rather than some other offset-from-UTC or time zone, it should carry a Z at the end. The Z is short for Zulu and means UTC.

    String input = "2002-05-30T09:30:10" + "Z" ;
    

    The java.time classes use the standard ISO 8601 formats by default when parsing/generating strings. So no need to specify a formatting pattern.

    Instant

    The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

    Instant instant = Instant.parse( "2002-05-30T09:30:10" + "Z" );
    

    instant.toString(): 2002-05-30T09:30:10Z

    ZonedDateTime

    If you want to see that same moment as the wall-clock time of a particular region, apply a ZoneId to get a ZonedDateTime object.

    Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as CET or EST or IST as they are not true time zones, not standardized, and not even unique(!).

    ZoneId z = ZoneId.of( "Europe/Paris" );
    ZonedDateTime zdt = instant.atZone( z );
    

    zdt.toString(): 2002-05-30T11:30:10+02:00[Europe/Paris]

    See this code run live at IdeOne.com.


    About java.time

    The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

    The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

    To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

    Where to obtain the java.time classes?

    • Java SE 8 and SE 9 and later
      • Built-in.
      • Part of the standard Java API with a bundled implementation.
      • Java 9 adds some minor features and fixes.
    • Java SE 6 and SE 7
      • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
    • Android
      • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
      • See How to use ThreeTenABP….

    The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

    0 讨论(0)
  • 2020-12-20 03:12

    Here is some code that could help you out with setting the timzones before parsing them:

    // sdf contains a Calendar object with the default timezone.
    Date date = new Date();
    String formatPattern = ....;
    SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
    
    TimeZone T1;
    TimeZone T2;
    ....
    ....
    // set the Calendar of sdf to timezone T1
    sdf.setTimeZone(T1);
    System.out.println(sdf.format(date));
    
    // set the Calendar of sdf to timezone T2
    sdf.setTimeZone(T2);
    System.out.println(sdf.format(date));
    
    // Use the 'calOfT2' instance-methods to get specific info
    // about the time-of-day for date 'date' in timezone T2.
    Calendar calOfT2 = sdf.getCalendar();
    

    another similar question I found might help too: How to set default time zone in Java and control the way date are stored on DB?

    EDIT:

    Here is a great tutorial on Java & Dates too: http://www.tutorialspoint.com/java/java_date_time.htm

    0 讨论(0)
提交回复
热议问题