java.util. to Utc Time via ZonedDateTime - Java java.time

Java examples for java.time:ZonedDateTime

Description

java.util. to Utc Time via ZonedDateTime

Demo Code


//package com.java2s;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import java.util.Date;

public class Main {
    private static final String UTC_TIME_ZONE = "Etc/UTC";

    public static Date toUtcTime(Date localTime) {
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(
                localTime.toInstant(), ZoneId.of(UTC_TIME_ZONE));

        return Date.from(zonedDateTime.toInstant());
    }//from  w  w  w  . j a  v a 2  s.c  o m
}

Related Tutorials