|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.time.Date
The Date class provides date and timestamp functionality as well as term and date maths. Date is in yyyymmdd order so that it sorts well. Date can be instantiated when you need a container for epoch time. It is used by the DAO to tag timestamps on records. It includes conversion from epochMilliseconds, a long, to epochSeconds, an integer. The Date object contains an elapsed time since the program started.
// Date object with conversion to and from seconds Date now = new Date(); int stamp = now.seconds(); Date recreation = new Date( stamp); Calendar calendar = calendar.setTimeInMillis( recreation.timeInMilliseconds); // often used as part of a file name to record the date created. int today = Date.dateStamp( System.currentTimeInMillis()); // yyyymmdd ie 20031129 today = Date.dateStamp( calendar); int dayStarted = Date.dateStamp( Date.getProgramStartTime()); stamp = Date.seconds( System.currentTimeInMillis()); // A full timestamp as used in logs and other readable text (yyyymmdd-hh:mm:ss.mmm) String timeStamp = Date.timeStamp( System.currentTimeInMillis()); int month = calendar.get( Calendar.MONTH); int year = calendar.get( Calendar.YEAR); int days = Date.daysInMonth( year, month); String message = days + " days in the month of "+months[month]+", "+year; Calendar commencementDate, expiryDate; expiryDate.setTime( commencementDate.getTime()); Date.add( expiryDate, termYears, termMonths, termDays); commencementDate.setTime( expiryDate.getTime()); Date.subtract( commencementDate, termYears, termMonths, termDays); Date.YMD term = Date.difference( commencementDate, expiryDate); termYears = term.years; termMonths = term.months; termDays = term.days;
| Nested Class Summary | |
static class |
Date.YMD
This class is used by the Date class for getting or working with terms - being the time between two date. |
| Field Summary | |
static int |
secondsInDay
Number of milliseconds in a day (for other calculations). |
long |
timeInMilliseconds
Time object set to in milliseconds since 1/1/1970 (epoch time). |
| Constructor Summary | |
Date()
Default constructor sets object to current time. |
|
Date(Calendar calendar)
Constructor, given a Calendar object. |
|
Date(Date like)
Copy constructor. |
|
Date(long timeInMilliseconds)
Set the date object from already available time. |
|
| Method Summary | |
Date |
add(int years,
int months,
int days)
Add a number of years, months and days to a date. |
Date |
add(Time interval)
Add a time interval (days, hour, minutes, seconds) to the current date. |
Calendar |
calendar()
Retrieve a Calendar object for this time. |
int |
compareTo(Object object)
|
int |
dateStamp()
returns datestamp for objects date (based on locale). |
static int |
daysInMonth(int year,
int month)
Return the number of days is a particular month. |
boolean |
equals(Object object)
|
static long |
getProgramStartTime()
Get the program start time in milliseconds since 1970. |
Date.YMD |
getTerm(Date to)
Calculate the difference between 2 days and return it as a term in years, months and days. |
int |
hashCode()
|
Date |
next(Time tod)
Sets Date object to the next time of day that matches the time object provided. |
int |
seconds()
Return the time as the number of seconds since January 1 1970. |
static int |
seconds(long currentTimeMillis)
Get the time from the epoch - being the number of milliseconds since January 1 1970. |
Date |
subtract(int years,
int months,
int days)
Remove a term in years, months and days from a date to get a starting date. |
Time |
timeOfDay()
Retrieve time of day for this date. |
String |
timeStamp()
Date/time stamp - typically used in logs. |
Date.YMD |
ymd()
Retrieve year/month/day components of date. |
| Methods inherited from class java.lang.Object |
getClass, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public final long timeInMilliseconds
public static final int secondsInDay
| Constructor Detail |
public Date()
public Date(long timeInMilliseconds)
timeInMilliseconds - since 1970 (epoch time).public Date(Date like)
like - Date to copy.public Date(Calendar calendar)
calendar - | Method Detail |
public int seconds()
public Date add(Time interval)
Date tomorrow5pm = new Date().add( new Time( 1, 17, 0, 0));
interval - Time object containing the interval.
public Date next(Time tod)
Date tomorrow5pm = new Date().setTOD(new Time(1, 17, 0, 0));
tod - Time object containing the time of day (or interval if set).
public Time timeOfDay()
public Calendar calendar()
public int dateStamp()
public String timeStamp()
public static long getProgramStartTime()
public static int seconds(long currentTimeMillis)
currentTimeMillis - time since 1970 (epoch time)
public static int daysInMonth(int year,
int month)
year - to calculate for (needed for leap years) - as in 2004.month - to return number of days in (0-11).
assertTrue( Date.daysInMonth( 2004, 1) == 29); assertTrue( Date.daysInMonth( 1900, 1) == 28);
public Date add(int years,
int months,
int days)
years - to add.months - to add.days - to add.
public Date subtract(int years,
int months,
int days)
years - to remove.months - to remove.days - to remove.
public Date.YMD ymd()
public Date.YMD getTerm(Date to)
to - later date
public boolean equals(Object object)
Object.equals(java.lang.Object)public int hashCode()
Object.hashCode()public int compareTo(Object object)
compareTo in interface ComparableComparable.compareTo(java.lang.Object)
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||