Search code examples
javadatetimecalendarepoch

Convert epoch to Date


I want to get Today's date in epoch or milisecs, but with the time part set to start of today i.e. 24:00(midnight)

For example

At the moment time is 10-APR-2013 17:56:44.455 which in

mili-seconds is 1365613004461
epoch = 1365613004

Can some one please tell me how to get epoch for 10-APR-2013 24:00:00?


Solution

  • You could set the time to 0:00:00 like this:

    Calendar cal = Calendar.getInstance();
    System.out.println(new SimpleDateFormat("dd-MM-yy HH:mm:ss.S").format(cal.getTime()));
    
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    
    System.out.println(new SimpleDateFormat("dd-MM-yy HH:mm:ss.S").format(cal.getTime()));
    

    The first value prints as 10-04-13 18:10:40.832 and the second 10-04-13 00:00:00.0.