Convert LocalDateTime to LocalDateTime in UTC

后端 未结 10 1000
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 08:20

Convert LocalDateTime to LocalDateTime in UTC.

LocalDateTime convertToUtc(LocalDateTime date) {

    //do conversion

}

I searched over net. Bu

相关标签:
10条回答
  • 2021-01-30 08:40

    There is an even simpler way

    LocalDateTime.now(Clock.systemUTC())
    
    0 讨论(0)
  • 2021-01-30 08:43

    Use the below. It takes the local datetime and converts it to UTC using the timezone. You do not need to create it function.

    ZonedDateTime nowUTC = ZonedDateTime.now(ZoneOffset.UTC);
    System.out.println(nowUTC.toString());
    

    If you need to obtain the LocalDateTime part of the ZonedDateTime then you can use the following.

    nowUTC.toLocalDateTime();
    

    Here is a static method i use in my application to insert UTC time in mysql since i cannot add a default value UTC_TIMESTAMP to a datetime column.

    public static LocalDateTime getLocalDateTimeInUTC(){
        ZonedDateTime nowUTC = ZonedDateTime.now(ZoneOffset.UTC);
    
        return nowUTC.toLocalDateTime();
    }
    
    0 讨论(0)
  • 2021-01-30 08:45

    Here's a simple little utility class that you can use to convert local date times from zone to zone, including a utility method directly to convert a local date time from the current zone to UTC (with main method so you can run it and see the results of a simple test):

    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZoneOffset;
    import java.time.ZonedDateTime;
    
    public final class DateTimeUtil {
        private DateTimeUtil() {
            super();
        }
    
        public static void main(final String... args) {
            final LocalDateTime now = LocalDateTime.now();
            final LocalDateTime utc = DateTimeUtil.toUtc(now);
    
            System.out.println("Now: " + now);
            System.out.println("UTC: " + utc);
        }
    
        public static LocalDateTime toZone(final LocalDateTime time, final ZoneId fromZone, final ZoneId toZone) {
            final ZonedDateTime zonedtime = time.atZone(fromZone);
            final ZonedDateTime converted = zonedtime.withZoneSameInstant(toZone);
            return converted.toLocalDateTime();
        }
    
        public static LocalDateTime toZone(final LocalDateTime time, final ZoneId toZone) {
            return DateTimeUtil.toZone(time, ZoneId.systemDefault(), toZone);
        }
    
        public static LocalDateTime toUtc(final LocalDateTime time, final ZoneId fromZone) {
            return DateTimeUtil.toZone(time, fromZone, ZoneOffset.UTC);
        }
    
        public static LocalDateTime toUtc(final LocalDateTime time) {
            return DateTimeUtil.toUtc(time, ZoneId.systemDefault());
        }
    }
    
    0 讨论(0)
  • 2021-01-30 08:49
    public static String convertFromGmtToLocal(String gmtDtStr, String dtFormat, TimeZone lclTimeZone) throws Exception{
            if (gmtDtStr == null || gmtDtStr.trim().equals("")) return null;
            SimpleDateFormat format = new SimpleDateFormat(dtFormat);
            format.setTimeZone(getGMTTimeZone());
            Date dt = format.parse(gmtDtStr);
            format.setTimeZone(lclTimeZone);
            return
    

    format.format(dt); }

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