Java calendar get the current date, without hours, minutes, seconds and milliseconds, in milliseconds

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
currentDate = cal.getTimeInMillis();

Be carefull on the timezone of your Calendar.


If you only want this relative to GMT and explicitly just want milliseconds, you can use this:

long now = System.currentTimeMillis();
long today = now - now % 86400000;

from the javadoc: The HOUR_OF_DAY, HOUR and AM_PM fields are handled independently and the the resolution rule for the time of day is applied. Clearing one of the fields doesn't reset the hour of day value of this Calendar. Use set(Calendar.HOUR_OF_DAY, 0) to reset the hour value.

cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

might need to set AM/PM as well

Tags:

Java

Calendar