Java Date UTC Parse toUTC(Date date)

Here you can find the source of toUTC(Date date)

Description

Extract date and time from the given date and returns a date object with the same date/time on the UTC time zone.

License

Open Source License

Parameter

Parameter Description
date a parameter

Declaration

public static Date toUTC(Date date) 

Method Source Code

//package com.java2s;
/**/*  w w w .j  a v a2  s .  c om*/
 * Copyright (C) 2015 https://github.com/ymanv
 *
 * This software is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**
     * Extract date and time from the given date and returns a date object with
     * the same date/time on the UTC time zone.
     *
     * @param date
     */
    public static Date toUTC(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        Calendar utcCal = Calendar.getInstance();
        utcCal.setTimeZone(TimeZone.getTimeZone("UTC"));

        utcCal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
        utcCal.set(Calendar.MILLISECOND, cal.get(Calendar.MILLISECOND));

        return utcCal.getTime();
    }
}

Related

  1. parseToUTCDate(String timestamp, String pattern)
  2. parseUTCDateToString(Date date)
  3. parseUTCToDate(String utcDate)
  4. toUTC(Date date)
  5. toUTC(Date date)
  6. uTCToChinaTS(Date date)
  7. UTCToDate(long utcValue)
  8. utcToLocal(Date utcDate)