Convert Date/Time for given Timezone - java

前端 未结 16 2521
孤城傲影
孤城傲影 2020-11-22 12:36

I want to convert this GMT time stamp to GMT+13:

2011-10-06 03:35:05

I have tried about 100 different combinations of DateFormat, TimeZone,

16条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 12:58

    Your approach works without any modification.

    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    // Timestamp for 2011-10-06 03:35:05 GMT
    calendar.setTime(new Date(1317872105000L));
    
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); 
    formatter.setTimeZone(TimeZone.getTimeZone("GMT+13"));
    
    // Prints 2011-10-06 16:35:05 GMT+13:00
    System.out.println(formatter.format(calendar.getTime()));
    

提交回复
热议问题