Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I would like to get the current date in milliseconds with only year, month and date. But when I use this code:

Calendar cal = Calendar.getInstance(); cal.clear(Calendar.HOUR); cal.clear(Calendar.MINUTE); cal.clear(Calendar.SECOND); cal.clear(Calendar.MILLISECOND); currentDate = cal.getTimeInMillis(); 

I still get the time in milliseconds with the hour. How can I fix this?

like image 592
just_user Avatar asked Oct 28 '11 14:10

just_user


People also ask

What does calendar getInstance () getTime () this return?

Calendar. getInstance(). getTime() : Returns a Date object representing this Calendar's time value (millisecond offset from the Epoch(January 1, 1970 00:00:00.000 GMT (Gregorian).)

How do you obtain the current second minute and hour in Java?

System. out. printf("%d-%02d-%02d %02d:%02d:%02d. %03d", year, month, day, hour, minute, second, millis);

How do you find the Date in milliseconds?

JavaScript - Date getMilliseconds() Method Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.


1 Answers

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.

like image 186
Fred Avatar answered Sep 19 '22 20:09

Fred