Java Calendar Millisecond calendarToGMTMillis(Calendar cal)

Here you can find the source of calendarToGMTMillis(Calendar cal)

Description

Convert a calendar object to a long in the form of milliseconds since the epoch of January 1, 1970.

License

Apache License

Parameter

Parameter Description
cal The calendar object

Return

The time in milliseconds

Declaration

public static long calendarToGMTMillis(Calendar cal) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*from  w  w w.j  a  v  a  2  s  .co  m*/
     * Convert a calendar object to a long in the form of milliseconds since the
     * epoch of January 1, 1970. The time is converted to GMT if it is not
     * already in that timezone so that all times will be in a standard
     * timezone.
     * 
     * @param cal
     *            The calendar object
     * @return The time in milliseconds
     */
    public static long calendarToGMTMillis(Calendar cal) {
        Date date = cal.getTime();
        TimeZone tz = cal.getTimeZone();
        int offset = tz.getOffset(date.getTime());
        long time;
        if (offset != 0) {
            Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
            gmtCal.setTime(date);
            gmtCal.add(Calendar.MILLISECOND, offset);
            time = gmtCal.getTimeInMillis();
        } else {
            time = date.getTime();
        }
        return time;
    }
}

Related

  1. calendar(long millis)
  2. calendarToGMTMillis(Calendar cal)
  3. clearCalendarMillisecond(Calendar cal)
  4. elapsedMillis(Calendar before, Calendar after)
  5. getCalendarXDaysFromY(long startTimeInMillis, int daysFromStartDate)
  6. getDateMilliseconds(Calendar pDate)