Wednesday, February 20, 2013

Get Timestamp for the Present Day in Java

// Snippet for getting timestamp in milliseconds for the present day with time taken as 00:00:00
// Eg: ts for '2013-02-19'
public long getTsForToday() {
Calendar cal = Calendar.getInstance();
int year = cal.get(cal.YEAR);
int month = cal.get(cal.MONTH);
int date = cal.get(cal.DATE);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, date);
return cal.getTimeInMillis();