> whats unix epoch time?
http://en.wikipedia.org/wiki/Unix_time
"The Unix epoch is the time 00:00:00 UTC on January 1, 1970. There is a problem with this definition, in that UTC did not exist in its current form until 1972; this issue is discussed below. For brevity, the remainder of this section will use ISO 8601 date format, in which the Unix epoch is 1970-01-01T00:00:00Z."
This should do what you're looking for:
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
public Main() {
}
public static void main(String[] args) {
final long MS_PER_DAY = 24 * 60 * 60 * 1000;
// Get the current time
Date now = new Date();
// Get a time from yesterday
Date yesterday = new Date(now.getTime() - MS_PER_DAY);
Calendar calendar = new GregorianCalendar();
calendar.setTime(yesterday);
// Get the time yesterday morning at 12:00AM
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date yesterdayStart = calendar.getTime();
// Get the time yesterday night at 11:59:59PM
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
Date yesterdayEnd = calendar.getTime();
System.out.println(
"Yesterday start: " + yesterdayStart +
"\t Since Epoch:" + yesterdayStart.getTime());
System.out.println(
"Yesterday end:" + yesterdayEnd +
"\t Since Epoch:" + yesterdayEnd.getTime());
}
}
If you want to be slightly more efficient, you could take the "yesterdayStart.getTime()" and add (MS_PER_DAY - 1000) to get the yesterdayEndTime (or, even better, use -1 instead of -1000 if you want to get all the way to 11:59:59:999).
One more question. I am trying to set the date to the last day of the last month. How can get the correct day since some months have 31 and others 30.
int prevMonth = calendar.get(Calendar.MONTH)-1;
calendar.set(Calendar.MONTH, prevMonth);
calendar.set(Calendar.DAY_OF_MONTH, 31);
is there a way to get and set the last day of a month?
Thanks
Ravi