Fetch Epoch time

Hi: I want to build a small java code that returns the unix epoch time for 12:00AM yesterday and 11:59PM yesterday. Anybody have code which does the above that you can share ?TIARavi
[203 byte] By [Rmalghana] at [2007-10-3 8:26:28]
# 1
whats unix epoch time?
mkoryaka at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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."

zadoka at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...
# 3

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

Jason_Waltona at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...
# 4
thanks jason
Rmalghana at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...
# 5

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

Rmalghana at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...
# 6
the following works calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(calendar.DAY_OF_MONTH));
Rmalghana at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...
# 7
Another way is to set the calendar to the First day the month and the use calendar.roll(Calendar.DATE,-1); To roll it back to the last day of the month within the same month.
Caffeine0001a at 2007-7-15 3:32:51 > top of Java-index,Java Essentials,Java Programming...