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

前端 未结 2 1920
清歌不尽
清歌不尽 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 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

提交回复
热议问题