Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.currentTimeMillis() for a given date? [duplicate]

Tags:

java

timestamp

Since java.util.Date is mostly deprecated, what's the right way to get a timestamp for a given date, UTC time? The one that could be compared against System.currentTimeMillis().

like image 405
yanchenko Avatar asked Mar 11 '09 00:03

yanchenko


1 Answers

Using the Calendar class you can create a time stamp at a specific time in a specific timezone. Once you have that, you can then get the millisecond time stamp to compare with:

Calendar cal = new GregorianCalendar();
cal.set(Calendar.DAY_OF_MONTH, 10);
// etc...

if (System.currentTimeMillis() < cal.getTimeInMillis()) {
  // do your stuff
}

edit: changed to use more direct method to get time in milliseconds from Calendar instance. Thanks Outlaw Programmer

like image 166
David Sykes Avatar answered Oct 10 '22 10:10

David Sykes