Prev: Open popup from JSP - Servlet ??
Next: XPath question.
From: laredotornado on 12 Mar 2010 11:12 Hi, I'm using Java 1.5. I have a java.util.Date object and I would like to determine if it's date (i.e. year, month, and day) are greater than (in the future) or equal to today's date (year, month, and day). However, I don't care about any time component (hour, minute, second ...) when the comparison is taking place. What is the easiest way I can determine this? Thanks, - Dave
From: Thomas Pornin on 12 Mar 2010 11:22 According to laredotornado <laredotornado(a)zipmail.com>: > I'm using Java 1.5. I have a java.util.Date object and I would like > to determine if it's date (i.e. year, month, and day) are greater than > (in the future) or equal to today's date (year, month, and day). > However, I don't care about any time component (hour, minute, > second ...) when the comparison is taking place. What is the easiest > way I can determine this? If you are in the UTC time zone (often called GMT too), then this is simple. Use this: public static int dayCount(Date d) { return (int)(d.getTime() / 86400000L); } which returns the date as an integral count of days since January 1st, 1970. You then just have to compare those day counts. For other time zones, you will have to resort to Calendar and TimeZone. Create a TimeZone instance for your time zone, then get a Calendar instance (with Calendar.getInstance(TimeZone)), and use it to convert your dates into years, months and days. --Thomas Pornin
From: laredotornado on 12 Mar 2010 11:56 On Mar 12, 9:22 am, Thomas Pornin <por...(a)bolet.org> wrote: > According to laredotornado <laredotorn...(a)zipmail.com>: > > > I'm using Java 1.5. I have a java.util.Date object and I would like > > to determine if it's date (i.e. year, month, and day) are greater than > > (in the future) or equal to today's date (year, month, and day). > > However, I don't care about any time component (hour, minute, > > second ...) when the comparison is taking place. What is the easiest > > way I can determine this? > > If you are in the UTC time zone (often called GMT too), then this > is simple. Use this: > > public static int dayCount(Date d) > { > return (int)(d.getTime() / 86400000L); > } > > which returns the date as an integral count of days since January 1st, > 1970. You then just have to compare those day counts. > > For other time zones, you will have to resort to Calendar and > TimeZone. Create a TimeZone instance for your time zone, then get > a Calendar instance (with Calendar.getInstance(TimeZone)), and use > it to convert your dates into years, months and days. > > --Thomas Pornin Thanks. This is just the simple solution I was looking for. One follow up . Why is the timezone important? If I know that both my Date objects are the same time zone, wouldn't it still work even if that time zone weren't GMT? - Dave
From: Nigel Wade on 12 Mar 2010 12:15 On Fri, 12 Mar 2010 08:56:32 -0800, laredotornado wrote: > On Mar 12, 9:22 am, Thomas Pornin <por...(a)bolet.org> wrote: >> According to laredotornado <laredotorn...(a)zipmail.com>: >> >> > I'm using Java 1.5. I have a java.util.Date object and I would like >> > to determine if it's date (i.e. year, month, and day) are greater >> > than (in the future) or equal to today's date (year, month, and day). >> > However, I don't care about any time component (hour, minute, second >> > ...) when the comparison is taking place. What is the easiest way I >> > can determine this? >> >> If you are in the UTC time zone (often called GMT too), then this is >> simple. Use this: >> >> public static int dayCount(Date d) >> { >> return (int)(d.getTime() / 86400000L); >> } >> >> which returns the date as an integral count of days since January 1st, >> 1970. You then just have to compare those day counts. >> >> For other time zones, you will have to resort to Calendar and TimeZone. >> Create a TimeZone instance for your time zone, then get a Calendar >> instance (with Calendar.getInstance(TimeZone)), and use it to convert >> your dates into years, months and days. >> >> --Thomas Pornin > > Thanks. This is just the simple solution I was looking for. One follow > up . Why is the timezone important? If I know that both my Date objects > are the same time zone, wouldn't it still work even if that time zone > weren't GMT? - Dave Because wall clock time (and this includes "what day is it" type questions) in one timezone is different from wall clock time in another timezone. Consider the two instants in time 1 minute before midnight GMT and 1 minute after midnight GMT. They belong to different days in London, but are both in the same day in New York (or any other timezone for that matter). Your two Dates are in GMT, but you may well not be. Asking "what day do these Dates represent" depends on what timezone you are in when you ask the question. Timezones are important. Ignore them at your peril. -- Nigel Wade
From: Patricia Shanahan on 12 Mar 2010 12:21
laredotornado wrote: > On Mar 12, 9:22 am, Thomas Pornin <por...(a)bolet.org> wrote: >> According to laredotornado <laredotorn...(a)zipmail.com>: >> >>> I'm using Java 1.5. I have a java.util.Date object and I would like >>> to determine if it's date (i.e. year, month, and day) are greater than >>> (in the future) or equal to today's date (year, month, and day). >>> However, I don't care about any time component (hour, minute, >>> second ...) when the comparison is taking place. What is the easiest >>> way I can determine this? >> If you are in the UTC time zone (often called GMT too), then this >> is simple. Use this: >> >> public static int dayCount(Date d) >> { >> return (int)(d.getTime() / 86400000L); >> } >> >> which returns the date as an integral count of days since January 1st, >> 1970. You then just have to compare those day counts. >> >> For other time zones, you will have to resort to Calendar and >> TimeZone. Create a TimeZone instance for your time zone, then get >> a Calendar instance (with Calendar.getInstance(TimeZone)), and use >> it to convert your dates into years, months and days. >> >> --Thomas Pornin > > Thanks. This is just the simple solution I was looking for. One > follow up . Why is the timezone important? If I know that both my > Date objects are the same time zone, wouldn't it still work even if > that time zone weren't GMT? - Dave It is a matter of setting appropriate boundaries between yesterday, today, and tomorrow. I'm writing this in California, Pacific Standard Time, in the morning. An event 10 hours ago happened late yesterday, from my point of view. Now consider someone in London. For them, it is now early evening and an event 10 hours ago happened this morning. Patricia |