Java – System.currentTimeMillis() for a given date?

javatimestamp

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().

Best Answer

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