de.laliluna.date
Class Date

java.lang.Object
  extended by de.laliluna.date.Date
All Implemented Interfaces:
java.io.Serializable, java.lang.Comparable<Date>

public class Date
extends java.lang.Object
implements java.lang.Comparable<Date>, java.io.Serializable

The central class of this API which allows to create dates, substract them, add time to a date and more convient functions. There are methods to calculate working days between two dates. A nice feature is the option to move a date by a fractions. 1/1 is the next day, 24/25 is a day and an hour. It is mutable and not thread safe.

See Also:
Serialized Form

Field Summary
static Fraction DAY
           
static Fraction HOUR
           
static Fraction MILLISECOND
           
static Fraction MINUTE
           
static Fraction SECOND
           
 
Constructor Summary
Date()
          Returns the current Date and Time, same as Date.now();
Date(java.util.Calendar calendar)
           
Date(Date date)
           
Date(java.util.Date date)
           
Date(int year, int month, int day)
           
Date(int year, int month, int day, int hour, int minute)
           
Date(int year, int month, int day, int hour, int minute, int second)
           
Date(int year, Months month, int day)
           
Date(int year, Months month, int day, int hour, int minute)
           
Date(int year, Months month, int day, int hour, int minute, int second)
           
Date(TimeZone timeZone)
           
 
Method Summary
 Date add(Fraction f)
          Adds time expressed as fraction add(new Fraction(1/1)) - adds a day add(new Fraction(1/24)) - adds an hour add(new Fraction(5/1440)) - adds 5 minutes The maximum you can add are 4085 years
 Date addDays(int day)
           
 Date addHours(int hours)
           
 Date addMilliSeconds(int milliSeconds)
           
 Date addMinutes(int minutes)
           
 Date addMonths(int month)
           
 Date addSeconds(int seconds)
           
 Date addYears(int years)
           
 boolean after(Date other)
           
 boolean afterDate(Date other)
           
 boolean afterTime(Date other)
           
 boolean am()
          Ante Meridiem or AM
 boolean before(Date other)
           
 boolean beforeDate(Date other)
           
 boolean beforeTime(Date other)
           
 int compareDateTo(java.util.Calendar other)
          Compares only the date part of a date to the other dates date part.
 int compareDateTo(Date other)
          Compares only the date part of a date to the other dates date part.
 int compareDateTo(java.util.Date other)
          Compares only the date part of a date to the other dates date part.
 int compareTimeTo(java.util.Calendar other)
          Compares only the time part of a date to the other dates time part.
 int compareTimeTo(Date other)
          Compares only the time part of a date to the other dates time part.
 int compareTimeTo(java.util.Date other)
          Compares only the time part of a date to the other dates time part.
 int compareTo(java.util.Calendar other)
          Compares date and time parts of a date to the other date
 int compareTo(Date other)
          Compares date and time parts of a date to the other date
 int compareTo(java.util.Date other)
          Compares date and time parts of a date to the other date
 int day()
          The day part of the date
 java.lang.String dayName()
          Prints an English name of the month.
 int dayOfWeek()
          Day of week is presented by 1 for Monday, 7 for Sunday, You can use the enum de.laliluna.date.Days to get a meaningful value
 java.lang.String dayShort()
          Prints a short English name of the day.
 boolean equalDate(Date other)
           
 boolean equalDateTime(Date other)
           
 boolean equals(java.lang.Object o)
           
 boolean equalTime(Date other)
           
 int hashCode()
           
 int hour()
          returns the hour from the time part.
 int hour12()
          returns the hours from the time part in a 0 to 12 range
 int millisecond()
          Milli seconds of the time, there is no guaranty that the time is measured precisely
 Fraction minus(Date date)
          Returns the time span between two dates as fraction 1/1 = 1 day, 1/24 = 1 hour, 26/24 = 1 day and 2 hours
 int minute()
          Minute part of the time
 int month()
          the month, 1 is January and 12 is December
 java.lang.String monthName()
          Prints an English name of the month.
 java.lang.String monthShort()
          Prints a short English name of the month.
static Date now()
          Creates a date and time instance for now
static Date now(TimeZone timeZone)
          Creates a date and time instance for now and sets the given timeZone
 boolean pm()
          Post Meridiem or PM
 Date resetDate()
          Sets year, month, day to zero
 Date resetTime()
          Sets hour, minute, second and millisecond to zero
 int second()
          Second part of the time
 Date setDay(int day)
           
 Date setHour(int hours)
           
 Date setMilliSeconds(int milliSeconds)
           
 Date setMinute(int minutes)
           
 Date setMonth(int month)
           
 Date setMonth(Months month)
           
 Date setSeconds(int seconds)
           
 Date setTimeZone(TimeZone timeZone)
           
 Date setYear(int year)
           
 Date substract(Fraction f)
          Subtracts a time span from the date
 long timeInMillis()
          Returns milli seconds since 1.1.1970 0:00:00 UTC
 java.util.Calendar toCalendar()
          Converts the date to java.util.Calendar
static Date today()
          Creates a date instance for todays date and sets the time to 00:00:00 000
static Date today(TimeZone timeZone)
          Creates a date instance for todays date and the given timeZone, sets the time to 00:00:00 000
 java.util.Date toJavaDate()
          Converts the date to java.util.Date
 java.lang.String toString()
           
 Date translate(TimeZone timeZone)
          Translates the current Date to the passed time zone.
 Fraction workingDaysBetween(Date other)
          Calculates the number of working days between to dates.
 Fraction workingDaysBetween(Date other, DayFilter... dayFilter)
          Calculates the number of working days between to dates.
 int year()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DAY

public static final Fraction DAY

HOUR

public static final Fraction HOUR

MINUTE

public static final Fraction MINUTE

SECOND

public static final Fraction SECOND

MILLISECOND

public static final Fraction MILLISECOND
Constructor Detail

Date

public Date()
Returns the current Date and Time, same as Date.now();


Date

public Date(TimeZone timeZone)

Date

public Date(int year,
            int month,
            int day)

Date

public Date(int year,
            Months month,
            int day)

Date

public Date(int year,
            int month,
            int day,
            int hour,
            int minute)

Date

public Date(int year,
            Months month,
            int day,
            int hour,
            int minute)

Date

public Date(int year,
            int month,
            int day,
            int hour,
            int minute,
            int second)

Date

public Date(int year,
            Months month,
            int day,
            int hour,
            int minute,
            int second)

Date

public Date(Date date)

Date

public Date(java.util.Calendar calendar)

Date

public Date(java.util.Date date)
Method Detail

today

public static Date today()
Creates a date instance for todays date and sets the time to 00:00:00 000

Returns:
the created instance

today

public static Date today(TimeZone timeZone)
Creates a date instance for todays date and the given timeZone, sets the time to 00:00:00 000

Returns:
the created instance

now

public static Date now()
Creates a date and time instance for now

Returns:
the created instance

now

public static Date now(TimeZone timeZone)
Creates a date and time instance for now and sets the given timeZone

Returns:
the created instance

resetDate

public Date resetDate()
Sets year, month, day to zero

Returns:
itself

resetTime

public Date resetTime()
Sets hour, minute, second and millisecond to zero

Returns:
itself

add

public Date add(Fraction f)
Adds time expressed as fraction add(new Fraction(1/1)) - adds a day add(new Fraction(1/24)) - adds an hour add(new Fraction(5/1440)) - adds 5 minutes The maximum you can add are 4085 years

Parameters:
f - - fraction to be added
Returns:
the same date, after the fraction was added

addYears

public Date addYears(int years)

addMonths

public Date addMonths(int month)

addDays

public Date addDays(int day)

addHours

public Date addHours(int hours)

addMinutes

public Date addMinutes(int minutes)

addSeconds

public Date addSeconds(int seconds)

addMilliSeconds

public Date addMilliSeconds(int milliSeconds)

setYear

public Date setYear(int year)

setMonth

public Date setMonth(int month)

setMonth

public Date setMonth(Months month)

setDay

public Date setDay(int day)

setHour

public Date setHour(int hours)

setMinute

public Date setMinute(int minutes)

setSeconds

public Date setSeconds(int seconds)

setMilliSeconds

public Date setMilliSeconds(int milliSeconds)

setTimeZone

public Date setTimeZone(TimeZone timeZone)

substract

public Date substract(Fraction f)
Subtracts a time span from the date

Parameters:
f - - the time span
Returns:
itself with the new time

minus

public Fraction minus(Date date)
Returns the time span between two dates as fraction 1/1 = 1 day, 1/24 = 1 hour, 26/24 = 1 day and 2 hours

Parameters:
date - - the date to be substracted
Returns:
the time span

workingDaysBetween

public Fraction workingDaysBetween(Date other)
Calculates the number of working days between to dates. Calculation follows the following logic: Working days are Mon to Fri. It works in the sense I am working from 4th of Feb to 6 of Feb => 3 days Working days between 4. Feb and 4. Feb is 1 Working days between 4. Feb and 6. Feb are 3

It will not recognize any holidays.

Parameters:
other - - the other date
Returns:
- number of working days as fraction 1/1 = 1 day, 3/1 = 3 days

workingDaysBetween

public Fraction workingDaysBetween(Date other,
                                   DayFilter... dayFilter)
Calculates the number of working days between to dates. Calculation follows the following logic: Working days are Mon to Fri. It works in the sense I am working from 4th of Feb to 6 of Feb => 3 days Working days between 4. Feb and 4. Feb is 1 Working days between 4. Feb and 6. Feb are 3

It will use the passed dayFilter to filter the working days. If you pass only one filter, then the filter will be asked for the total days. If you pass multiple filters, all days returned from the filter are collected and the total of unique days is substracted. You could create for example a filter to return all free days on a workingday.

Parameters:
other - - the other date
Returns:
- number of working days as fraction 1/1 = 1 day, 3/1 = 3 days

translate

public Date translate(TimeZone timeZone)
Translates the current Date to the passed time zone.

Parameters:
timeZone - - the target time zone
Returns:
itself

year

public int year()
Returns:
- the year

month

public int month()
the month, 1 is January and 12 is December

Returns:
- the month

day

public int day()
The day part of the date

Returns:

dayOfWeek

public int dayOfWeek()
Day of week is presented by 1 for Monday, 7 for Sunday, You can use the enum de.laliluna.date.Days to get a meaningful value

Returns:
the day of the week

hour

public int hour()
returns the hour from the time part. Range is between 0 and 23

Returns:
the hour

hour12

public int hour12()
returns the hours from the time part in a 0 to 12 range

Returns:
the hour

am

public boolean am()
Ante Meridiem or AM

Returns:
true if the time is before 12:00:00 and after 23:59:59 999

pm

public boolean pm()
Post Meridiem or PM

Returns:
true if the time is between 12:00:00 and 23:59:59

minute

public int minute()
Minute part of the time

Returns:
int value in minutes

second

public int second()
Second part of the time

Returns:
int value in seconds

millisecond

public int millisecond()
Milli seconds of the time, there is no guaranty that the time is measured precisely

Returns:
int value in milli seconds

compareTo

public int compareTo(Date other)
Compares date and time parts of a date to the other date

Specified by:
compareTo in interface java.lang.Comparable<Date>
Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareTo

public int compareTo(java.util.Date other)
Compares date and time parts of a date to the other date

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareTo

public int compareTo(java.util.Calendar other)
Compares date and time parts of a date to the other date

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareDateTo

public int compareDateTo(Date other)
Compares only the date part of a date to the other dates date part. (= It ignores the time)

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareDateTo

public int compareDateTo(java.util.Date other)
Compares only the date part of a date to the other dates date part. (= It ignores the time)

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareDateTo

public int compareDateTo(java.util.Calendar other)
Compares only the date part of a date to the other dates date part. (= It ignores the time)

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareTimeTo

public int compareTimeTo(Date other)
Compares only the time part of a date to the other dates time part.

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareTimeTo

public int compareTimeTo(java.util.Date other)
Compares only the time part of a date to the other dates time part.

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

compareTimeTo

public int compareTimeTo(java.util.Calendar other)
Compares only the time part of a date to the other dates time part.

Parameters:
other - - date to compare with
Returns:
1 if after, 0 if equal and -1 if before the other date

before

public boolean before(Date other)

beforeDate

public boolean beforeDate(Date other)

beforeTime

public boolean beforeTime(Date other)

after

public boolean after(Date other)

afterDate

public boolean afterDate(Date other)

afterTime

public boolean afterTime(Date other)

equalDateTime

public boolean equalDateTime(Date other)

equalDate

public boolean equalDate(Date other)

equalTime

public boolean equalTime(Date other)

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toCalendar

public java.util.Calendar toCalendar()
Converts the date to java.util.Calendar

Returns:
- the date as Calendar

toJavaDate

public java.util.Date toJavaDate()
Converts the date to java.util.Date

Returns:
- the date as java.util.Date

timeInMillis

public long timeInMillis()
Returns milli seconds since 1.1.1970 0:00:00 UTC

Returns:
milli seconds

monthShort

public java.lang.String monthShort()
Prints a short English name of the month. The values are taken from Months.shortNames

Returns:
- the name

monthName

public java.lang.String monthName()
Prints an English name of the month. The values are taken from Months.names

Returns:
- the name

dayShort

public java.lang.String dayShort()
Prints a short English name of the day. The values are taken from Days.shortNames

Returns:
- the name

dayName

public java.lang.String dayName()
Prints an English name of the month. The values are taken from Days.names

Returns:
- the name

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © 2009 Sebastian Hennebrueder, http://www.laliluna.de