Timezone conversion

后端 未结 12 1540
一向
一向 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 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());
    }
    

提交回复
热议问题