|
It really depends on what you mean by "default date". Do you want to have the current date put in your variable? Tomorrows date? Yesterdays date? Below is an "ease of use" class for dates. import java.util.Date; import java.util.Calendar; import java.util.TimeZone; /** * At the moment of writing rapla internaly stores all appointments in the GMT * timezone. */ public abstract class DateTools { public static final int DAYS_PER_WEEK = 7; public static final long MILLISECONDS_PER_MINUTE = 1000 * 60; public static final long MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * 60; public static final long MILLISECONDS_PER_DAY = 24 * MILLISECONDS_PER_HOUR; public static final long MILLISECONDS_PER_WEEK = 7 * MILLISECONDS_PER_DAY; public static int getHourOfDay(long date) { return (int) ((date % MILLISECONDS_PER_DAY) / MILLISECONDS_PER_HOUR); } public static int getMinuteOfHour(long date) { return (int) ((date % MILLISECONDS_PER_HOUR) / MILLISECONDS_PER_MINUTE); } /** * sets time of day to 0:00. * * @see #cutDate(Date) */ public static long cutDate(long date) { return (date - (date % MILLISECONDS_PER_DAY)); } /** * sets time of day to 0:00. * * @see #cutDate(Date) */ public static void cutDate(Calendar calendar) { calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); } /** sets time of day to 0:00. */ public static Date cutDate(Date date) { return new Date(cutDate(date.getTime())); } static TimeZone timeZone = TimeZone.getTimeZone("GMT"); /** same as TimeZone.getTimeZone("GMT"). */ public static TimeZone getTimeZone() { return timeZone; } /** * sets time of day to 0:00 and increases day. * * @see #fillDate(Date) */ public static long fillDate(long date) { return cutDate(date) + MILLISECONDS_PER_DAY; } public static Date fillDate(Date date) { return new Date(fillDate(date.getTime())); } /** * Monday 24:00 = tuesday 0:00. But the first means end of monday and the * second start of tuesday. The default DateFormat always displays tuesday. * If you want to edit the first interpretation in calendar components. call * addDay() to add 1 day to the given date before displaying and subDay() * for mapping a day back after editing. * * @see #subDay * @see #addDays */ public static Date addDay(Date date) { return new Date(date.getTime() + MILLISECONDS_PER_DAY); } /** see #addDay */ public static Date addDays(Date date, int days) { return new Date(date.getTime() + MILLISECONDS_PER_DAY * days); } /** * @see #addDay * @see #subDays */ public static Date subDay(Date date) { return new Date(date.getTime() - MILLISECONDS_PER_DAY); } /** * @see #addDay */ public static Date subDays(Date date, int days) { return new Date(date.getTime() - MILLISECONDS_PER_DAY * days); } /** * returns if the two dates are one the same date. Dates must be in GMT */ static public boolean isSameDay(long d1, long d2) { return cutDate(d1) == cutDate(d2); } /** * uses the calendar-object for date comparison. Use this for non GMT Dates */ static public boolean isSameDay(Calendar calendar, Date d1, Date d2) { calendar.setTime(d1); int era1 = calendar.get(Calendar.ERA); int year1 = calendar.get(Calendar.YEAR); int day_of_year1 = calendar.get(Calendar.DAY_OF_YEAR); calendar.setTime(d2); int era2 = calendar.get(Calendar.ERA); int year2 = calendar.get(Calendar.YEAR); int day_of_year2 = calendar.get(Calendar.DAY_OF_YEAR); return (era1 == era2 && year1 == year2 && day_of_year1 == day_of_year2); } } -----Original Message----- From: java400-l-bounces@xxxxxxxxxxxx [mailto:java400-l-bounces@xxxxxxxxxxxx] On Behalf Of RPower@xxxxxxxxxx Sent: Friday, June 17, 2005 12:03 PM To: Java Programming on and around the iSeries / AS400 Subject: RE: Date() Hmm.. ok. Looks like I'd better put some coding to test for null then... Ron Power Programmer Information Services City Of St. John's, NL P.O. Box 908 St. John's, NL A1C 5M2 Tel: 709-576-8132 Email: rpower@xxxxxxxxxx Website: http://www.stjohns.ca/ ___________________________________________________________________________ Success is going from failure to failure without a loss of enthusiasm. - Sir Winston Churchill
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.