Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Get Current Date (with time being 0:00:000) in Milliseconds?

Tags:

date

android

time

I would like to get the current date with the time zeroed out in milliseconds.

Example, if it's 12:69pm today, I want to get the time in milliseconds for today's date with no time...meaning, the time just after midnight (one millisecond or 0 if that works).

I was using the Calendar object but can't seem to figure out how to zero out the time portion.

like image 630
Camille Sévigny Avatar asked Dec 07 '11 13:12

Camille Sévigny


Video Answer


2 Answers

Here is how to zero the time of a calendar:

Calendar today = Calendar.getInstance();
today.set(Calendar.MILLISECOND, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.HOUR_OF_DAY, 0);
like image 99
Jave Avatar answered Oct 13 '22 00:10

Jave


And without calendar:

long d = new Date().getTime();
int offset = TimeZone.getDefault().getOffset(d);
d = ((d + offset)/ 86400000l) * 86400000l - offset;
like image 36
muffinmad Avatar answered Oct 13 '22 00:10

muffinmad