Java Calendar Millisecond translateToMilliseconds(Calendar calendar)

Here you can find the source of translateToMilliseconds(Calendar calendar)

Description

Translate the calendar to a number of millisec since 01/01/2000

License

Open Source License

Declaration

public static long translateToMilliseconds(Calendar calendar) 

Method Source Code


//package com.java2s;
/*/*  w ww .j  a  va  2  s. c om*/
Copyright 2005-2015, Olivier PETRUCCI.
    
This file is part of Areca.
    
Areca is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
Areca 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 General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Areca; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    
*/

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    private static int ZERO_YEAR = 2000;
    private static int[] CUMUL_DAYS_PER_YEAR = { 0, 366, 731, 1096, 1461, 1827, 2192, 2557, 2922, 3288, 3653, 4018,
            4383, 4749, 5114, 5479, 5844, 6210, 6575, 6940, 7305, 7671, 8036, 8401, 8766, 9132, 9497, 9862, 10227 };

    /**
     * Translate the calendar to a number of millisec since 01/01/2000
     */
    public static long translateToMilliseconds(Calendar calendar) {
        return translateToDayNumber(calendar) * 86400000 + calendar.get(Calendar.HOUR_OF_DAY) * 3600000
                + calendar.get(Calendar.MINUTE) * 60000 + calendar.get(Calendar.SECOND) * 1000
                + calendar.get(Calendar.MILLISECOND);
    }

    /**
     * 
     * @param y
     * @param m Month number, 1=Jan, 2=Feb, ..., 12=Dec
     * @param d
     * @return
     */
    public static int translateToDayNumber(int y, int m, int d) {
        Calendar cal = new GregorianCalendar(y, m - 1, d);
        return translateToDayNumber(cal);
    }

    /**
     * Translate the calendar to a number of days since 01/01/2000
     */
    public static int translateToDayNumber(Calendar cal) {
        if (cal == null) {
            return 0;
        } else {
            int y = cal.get(cal.YEAR) - ZERO_YEAR;

            int nbD = CUMUL_DAYS_PER_YEAR[y];
            nbD += cal.get(Calendar.DAY_OF_YEAR) - 1;

            return nbD;
        }
    }
}

Related

  1. setCalendarTime(Calendar inCalendar, int hr, int min, int sec, int milliSec)
  2. setTimeInMillis(Calendar _calendar, long _timemillis)
  3. toCalendar(final long millis)
  4. toCalendar(long millis)
  5. toStringDateTimeMillisecond(Calendar calendar)