org.cip4.jdflib.util
Class JDFDate

java.lang.Object
  extended by org.cip4.jdflib.util.JDFDate
All Implemented Interfaces:
Cloneable, Comparable<Object>, Comparator<JDFDate>

public class JDFDate
extends Object
implements Comparable<Object>, Cloneable, Comparator<JDFDate>

class to manipulate date and time according to ISO 8601
the date and time are in local time with the respective time zone specified

Author:
Rainer Prosi, Heidelberger Druckmaschinen

Field Summary
static String DATENUMERIC_DDMM
           
static String DATENUMERIC_MMDD
           
static String DATETIMEISO
           
static String DATETIMEISO_0
          iso - seconds are 0
static String DATETIMEISO_00
          iso - seconds + minutes are 0
static String DATETIMEISO_000
          iso - seconds + minutes + hours are 0
static String DATETIMENUMERIC_DDMM
           
static String DATETIMENUMERIC_MMDD
           
static String DATETIMEREADABLE
           
 
Constructor Summary
JDFDate()
          Allocates a JDFDate object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
JDFDate(JDFDate other)
           
JDFDate(long iTime)
          Allocates a JDFDate object and initializes it so that it represents the time point, expressed in milliseconds after January 1, 1970, 0:00:00 GMT.
JDFDate(String strDateTime)
          Allocates a JDFDate object and initializes it so that the JDFDate represents a date set by strDateTime Format of strDateTime Valid DataTime Strings are: "yyyy-mm-ddThh:mm:ss.sss+hh:00" "yyyy-mm-ddThh:mm:ss+hh:00" "yyyy-mm-ddThh:mm:ss-hh:00" "yyyy-mm-ddThh:mm:ssZ" Attention!
 
Method Summary
 void addOffset(int seconds, int minutes, int hours, int days)
          add a given offset to this
note: multiple calls stack
 boolean after(JDFDate other)
          true, if this is after other, also true if other==null
 boolean after(long other)
          true, if this is after other
 boolean before(JDFDate other)
          true, if this is before other, also true if other==null
 boolean before(long other)
          true, if this is before other
 JDFDate clone()
           
 int compare(JDFDate d0, JDFDate d1)
           
 int compareTo(Object arg0)
           
static JDFDate createDate(String date)
          factory style constructor that catches all exceptions and returns null if date is invalid
 JDFDate createDateFromDuration(JDFDuration duration, int hour, int minute)
          create a date with a relative offset defined in duration
 boolean equals(Object other)
          Compares two JDFDates for equality.
 GregorianCalendar getCalendar()
           
 String getDateISO()
          the date formated as defined in ISO 8601
the date is in local time with the respective time zone specified
 String getDateTime()
          returns the date and time of this in none ISO pattern 'yyyyMMddHHmmss'
 String getDateTimeISO()
          format the date and time as an ISO 8601 conform String
the date and time are in local time with the respective time zone specified
 String getDateTimeISOBD()
          Deprecated. use @see getDateISO()
 int getDay()
          get the day of month as an integer
 String getFormattedDateTime(String format)
          returns the date and time of this in nonean arbitrary pattern
 int getMonth()
          get the month as an integer
 Date getTime()
          Deprecated.  
 long getTimeInMillis()
          get the time in milliseconds
 String getTimeISO()
          format the time into a ISO conform String
the time is in local time
 String getTimeZoneISO()
          the TimeZone as spezified in ISO 8601 +/-10:00 for example
 int getTimeZoneOffsetInMillis()
           
 int getYear()
          get the year as an integer
 int hashCode()
          hashCode complements equals() to fulfill the equals/hashCode contract
 boolean isEarlier(JDFDate x)
          Tests if this date is before the specified date.
 boolean isLater(JDFDate x)
          Tests if this date is after the specified date.
static void setDefaultHour(int defaultHour)
          Setter for defaultHour attribute.
 void setOffset(int iOffset)
          Deprecated. use addOffset
 void setTime(Date date)
          Deprecated.  
 void setTime(int h, int m, int s)
          set the time without modifying the date
 void setTimeInMillis(long l)
          set this time milliseconds
 void setTimeZoneOffsetInMillis(int timeZoneOffsetInMillis)
           
 String toString()
          for debug purpose
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DATETIMEISO

public static final String DATETIMEISO
See Also:
Constant Field Values

DATETIMEISO_0

public static final String DATETIMEISO_0
iso - seconds are 0

See Also:
Constant Field Values

DATETIMEISO_00

public static final String DATETIMEISO_00
iso - seconds + minutes are 0

See Also:
Constant Field Values

DATETIMEISO_000

public static final String DATETIMEISO_000
iso - seconds + minutes + hours are 0

See Also:
Constant Field Values

DATETIMEREADABLE

public static final String DATETIMEREADABLE
See Also:
Constant Field Values

DATENUMERIC_DDMM

public static final String DATENUMERIC_DDMM
See Also:
Constant Field Values

DATENUMERIC_MMDD

public static final String DATENUMERIC_MMDD
See Also:
Constant Field Values

DATETIMENUMERIC_DDMM

public static final String DATETIMENUMERIC_DDMM
See Also:
Constant Field Values

DATETIMENUMERIC_MMDD

public static final String DATETIMENUMERIC_MMDD
See Also:
Constant Field Values
Constructor Detail

JDFDate

public JDFDate()
Allocates a JDFDate object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond. Also sets the current time zone to the system default time zone


JDFDate

public JDFDate(long iTime)
Allocates a JDFDate object and initializes it so that it represents the time point, expressed in milliseconds after January 1, 1970, 0:00:00 GMT. Also sets the current time zone to the system default time zone

Parameters:
iTime - current time in milliseconds after January 1, 1970, 0:00:00 GMT. Use JDFDuration instead. This class will be modified to handle only JDFDate objects

JDFDate

public JDFDate(JDFDate other)
Parameters:
other - the date to clone

JDFDate

public JDFDate(String strDateTime)
        throws DataFormatException
Allocates a JDFDate object and initializes it so that the JDFDate represents a date set by strDateTime Format of strDateTime

Valid DataTime Strings are:

  • "yyyy-mm-ddThh:mm:ss.sss+hh:00"
  • "yyyy-mm-ddThh:mm:ss+hh:00"
  • "yyyy-mm-ddThh:mm:ss-hh:00"
  • "yyyy-mm-ddThh:mm:ssZ"
  • Attention!
    you can enter milliseconds, but getDateTimeISO() still returns the time rounded to full seconds. Only long getTimeInMillis() returns the exact time

    Parameters:
    strDateTime - formatted date and time
    Throws:
    DataFormatException - if strDateTime is not a valid DateTime Attention! you can enter milliseconds, but getDateTimeISO() still returns the time rounded to full seconds only long getTimeInMillis() returns the exact time
    Method Detail

    setDefaultHour

    public static void setDefaultHour(int defaultHour)
    Setter for defaultHour attribute.

    Parameters:
    defaultHour - the defaultHour to set

    createDate

    public static JDFDate createDate(String date)
    factory style constructor that catches all exceptions and returns null if date is invalid

    Parameters:
    date - the formatted date string
    Returns:
    the JDFDate , null if date is not a valid string

    toString

    public String toString()
    for debug purpose

    Overrides:
    toString in class Object
    Returns:
    Object informations

    getFormattedDateTime

    public String getFormattedDateTime(String format)
    returns the date and time of this in nonean arbitrary pattern

    Parameters:
    format - the format string using FastDateFormat formatting
    Returns:
    String - the date as specified by the pattern
    Throws:
    IllegalArgumentException

    getDateTime

    public String getDateTime()
    returns the date and time of this in none ISO pattern 'yyyyMMddHHmmss'

    Returns:
    String - the date in pattern yyyyMMddHHmmss

    setOffset

    @Deprecated
    public void setOffset(int iOffset)
    Deprecated. use addOffset

    setOffset: set the offset to this time. Note: The time stored in this is not resetted if you want an offset based on current time use 'public MyDate(int iOffset)'

    Parameters:
    iOffset - offset time in seconds

    addOffset

    public void addOffset(int seconds,
                          int minutes,
                          int hours,
                          int days)
    add a given offset to this
    note: multiple calls stack

    Parameters:
    seconds - seconds to add to this
    minutes - minutes to add to this
    hours - hours to add to this
    days - days to add to this

    createDateFromDuration

    public JDFDate createDateFromDuration(JDFDuration duration,
                                          int hour,
                                          int minute)
    create a date with a relative offset defined in duration

    Parameters:
    duration -
    hour - the fixed hour, if -1 don't set
    minute - the fixed minute, if -1 don't set
    Returns:

    getDateTimeISO

    public String getDateTimeISO()
    format the date and time as an ISO 8601 conform String
    the date and time are in local time with the respective time zone specified

    Returns:
    date and time as String of form yyyy-mm-ddThh:mm:ss+zz:00

    getDateTimeISOBD

    @Deprecated
    public String getDateTimeISOBD()
    Deprecated. use @see getDateISO()

    format the date with no time added

    Returns:
    date and time as String of form yyyy-mm-dd

    getDateISO

    public String getDateISO()
    the date formated as defined in ISO 8601
    the date is in local time with the respective time zone specified

    Returns:
    String: the date of this of form yyyy-mm-dd

    getTimeISO

    public String getTimeISO()
    format the time into a ISO conform String
    the time is in local time

    Returns:
    String: the time of this ISO 8601 in format hh:mm:ss

    getTimeZoneISO

    public String getTimeZoneISO()
    the TimeZone as spezified in ISO 8601 +/-10:00 for example

    Returns:
    String: the timezone

    isLater

    public boolean isLater(JDFDate x)
    Tests if this date is after the specified date.

    Parameters:
    x - the date you wish to know if it is later than this
    Returns:
    true if and only if the instant represented by this Date object is strictly later than the instant represented by x; false otherwise.

    isEarlier

    public boolean isEarlier(JDFDate x)
    Tests if this date is before the specified date.

    Parameters:
    x - the date you wish to know if it is eariler than this
    Returns:
    true if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by x; false otherwise.

    getTimeInMillis

    public long getTimeInMillis()
    get the time in milliseconds

    Returns:
    long - the time in milliseconds

    setTimeInMillis

    public void setTimeInMillis(long l)
    set this time milliseconds

    Parameters:
    l - time in milliseconds

    getCalendar

    public GregorianCalendar getCalendar()
    Returns:
    the GregorianCalendar for this date

    setTime

    public void setTime(int h,
                        int m,
                        int s)
    set the time without modifying the date

    Parameters:
    h -
    m -
    s -

    before

    public boolean before(JDFDate other)
    true, if this is before other, also true if other==null

    Parameters:
    other - JDFDate to compare
    Returns:
    true if this is before other

    before

    public boolean before(long other)
    true, if this is before other

    Parameters:
    other - the time in milliseconds since 1970 (e.g. from System.currentTimeMillis())
    Returns:
    true if this is before other

    after

    public boolean after(JDFDate other)
    true, if this is after other, also true if other==null

    Parameters:
    other -
    Returns:
    true if this date is after other

    after

    public boolean after(long other)
    true, if this is after other

    Parameters:
    other - the time in milliseconds since 1970 (e.g. from System.currentTimeMillis())
    Returns:
    true if this date is after other

    getTime

    @Deprecated
    public Date getTime()
    Deprecated. 

    why the %&$/ is this called getTime, when it returns a date???

    Returns:

    getMonth

    public int getMonth()
    get the month as an integer

    Returns:
    (1-12)

    getYear

    public int getYear()
    get the year as an integer

    Returns:
    the year

    getDay

    public int getDay()
    get the day of month as an integer

    Returns:
    the year

    setTime

    @Deprecated
    public void setTime(Date date)
    Deprecated. 

    why the %&$/ is this called setTime, when it uses a date???

    Parameters:
    date -

    equals

    public boolean equals(Object other)
    Compares two JDFDates for equality.
    The result is true if and only if the argument is not null and is a JDFDate object that represents the same point in time, to the millisecond, as this object.

    Thus, two JDFDate objects are equal if and only if the getTimeInMillis method returns the same long value for both.

    Specified by:
    equals in interface Comparator<JDFDate>
    Overrides:
    equals in class Object

    hashCode

    public int hashCode()
    hashCode complements equals() to fulfill the equals/hashCode contract

    Overrides:
    hashCode in class Object

    compareTo

    public int compareTo(Object arg0)
    Specified by:
    compareTo in interface Comparable<Object>
    See Also:
    the value 0 if the argument is a Date equal to this Date; a value less than 0 if the argument is a Date after this Date; and a value greater than 0 if the argument is a Date before this Date.

    setTimeZoneOffsetInMillis

    public void setTimeZoneOffsetInMillis(int timeZoneOffsetInMillis)
    Parameters:
    timeZoneOffsetInMillis - The timeZoneOffsetInMillis to set.

    getTimeZoneOffsetInMillis

    public int getTimeZoneOffsetInMillis()
    Returns:
    Returns the timeZoneOffsetInMillis.

    clone

    public JDFDate clone()
    Overrides:
    clone in class Object
    See Also:
    Object.clone()

    compare

    public int compare(JDFDate d0,
                       JDFDate d1)
    Specified by:
    compare in interface Comparator<JDFDate>
    Parameters:
    d0 -
    d1 -
    Returns:
    See Also:
    Comparator.compare(java.lang.Object, java.lang.Object)


    Copyright © 2013. All Rights Reserved.