Java Date Now getCurrentDateInMillis()

Here you can find the source of getCurrentDateInMillis()

Description

get Current Date In Millis

License

Apache License

Declaration

public static long getCurrentDateInMillis() 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static long getCurrentDateInMillis() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTimeInMillis();
    }/* w  ww  . jav a  2 s. c o  m*/

    public static long getTimeInMillis(String dateString, String format) {
        long ts = 0L;

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);

            Date date = sdf.parse(dateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);

            ts = calendar.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ts;
    }
}

Related

  1. getCurrentDateAsString(String timestampFormat)
  2. getCurrentDateByString(String datePattern)
  3. getCurrentDateDBFormat()
  4. getCurrentDateFormat()
  5. getCurrentDateFormated()
  6. getCurrentDateInSqlFormat()
  7. getCurrentDateINYYYY_MM_DD()
  8. getCurrentDateInyyyyMMddFormat()
  9. getCurrentDateINYYYYMMDDHHMMSSwithColons()