Timezone conversion

后端 未结 12 1423
一向
一向 2020-11-22 11:25

I need to convert from one timezone to another timezone in my project.

I am able to convert from my current timezone to another but not from a different timezone to

相关标签:
12条回答
  • 2020-11-22 11:55

    here a story: my user in US enters a date in a web page. My server gets this as a java.util.Date object. Date objects have no notion of time zone.

    so let's say user entered 11PM(== 4AM london time). For her this was 11PM US time. Your server gets this and interprets this as 11PM of JVM's timezone. but what you need is a Date object that represents 4AM.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String timeStringInUS =  sdf.format("2020-05-04 23:00:00");
    
    SimpleDateFormat dateFormatInUS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat dateFormatInUK = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    dateFormatInUS.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    dateFormatInUK.setTimeZone(TimeZone.getTimeZone("Europe/London"));
    
    Date dateInUS = dateFormatInUS.parse(timeStringInUS);
    Date dateInUK = sdf.parse(dateFormatInUK.format(dateInUS));
    
    0 讨论(0)
  • 2020-11-22 11:57
        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();
    
    0 讨论(0)
  • 2020-11-22 12:02

    Depends on what you really mean by "converting".

    It MAY be as simple as setting the time zone in the FORMATTER, and not mucking with Calendar at all.

    Calendar cal = Calendar.getInstance();
    
    TimeZone tzUTC = TimeZone.getTimeZone( "UTC" );
    TimeZone tzPST = TimeZone.getTimeZone( "PST8PDT" );
    
    DateFormat dtfmt = new SimpleDateFormat( "EEE, yyyy-MM-dd KK:mm a z" );
    
    dtfmt.setTimeZone( tzUTC );
    System.out.println( "UTC: " + dtfmt.format( cal.getTime() ));
    
    dtfmt.setTimeZone( tzPST );
    System.out.println( "PST: " + dtfmt.format( cal.getTime() ));
    
    0 讨论(0)
  • 2020-11-22 12:08

    You could use the java.time.ZoneDateTime#ofInstant() method:

    import java.time.*;
    
    public class TimeZonesConversion {
        static ZonedDateTime convert(ZonedDateTime time, ZoneId newTimeZone) {
            return ZonedDateTime.ofInstant(
                    time.toInstant(),
                    newTimeZone);
        };
    
        public static void main(String... args) {
            ZonedDateTime mstTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("-07")); 
            ZonedDateTime localTime = convert(mstTime, Clock.systemDefaultZone().getZone());
            System.out.println("MST(" + mstTime + ") = " + localTime);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 12:09

    The "default" time zone can be avoided entirely by just setting the time zone appropriately for the Calendar object. However, I would personally suggest that you use Joda Time as a far superior API for date and time operations in Java. Amongst other things, time zone conversion is very simple in Joda.

    It's not clear what your current code looks like and why you're only able to convert via the default time zone, but in Joda Time you'd just specify the time zone explicitly when creating (say) a DateTime object, and then use withZone(DateTimeZone zone).

    If you could tell us more about how you're getting input data, we could give a fuller example.

    0 讨论(0)
  • 2020-11-22 12:09

    This is not the answer, but could help someone trying to generate dates with same timezone and apply another timezone's offset. It is useful when your application server is running in one timezone and your database in another.

    public static Date toGreekTimezone (Date date) {
      ZoneId greek = ZoneId.of(EUROPE_ATHENS);
      ZonedDateTime greekDate = ZonedDateTime.ofInstant(date.toInstant(), greek);
    
      ZoneId def = ZoneId.systemDefault();
      ZonedDateTime defDate = greekDate.withZoneSameLocal(def);
    
      return Date.from(defDate.toInstant());
    }
    
    0 讨论(0)
提交回复
热议问题