com.perdues
Class StyleDate

java.lang.Object
  |
  +--com.perdues.StyleDate

public class StyleDate
extends java.lang.Object

The StyleDate class makes it convenient to combine Calendar and time arithmetic with formatting. Combines the functions of java.util.Date, java.sql.Date, SimpleDateFormat, and Calendar with a single interface.

A StyleDate maintains a current date/time, and provides methods to set the various date/time fields and to add (or subtract) from its current value. The toString method formats the current date/time according to the current format.

You can set the day within the current week, the day of the month, the month, or the year directly, and set the time to the beginning of the day. You can add positive or negative numbers of days, months, years, or milliseconds to the internal date/time with calendar-based results.

All the calendar fields are available with normalized data at all times. For example, if you set the date/time to the beginning of the month, then add a month, the date/time is the beginning of the next month.

The TimeZone and Locale are configurable as well as the format.


Constructor Summary
StyleDate()
          Creates a StyleDate with the current date and time.
StyleDate(java.util.Date d)
          Creates a StyleDate for the given Date.
StyleDate(java.util.Date d, java.lang.String fmt)
          Create a StyleDate with a particular format and Date.
StyleDate(long millis)
          Creates a StyleDate for the given absolute time in milliseconds.
StyleDate(java.lang.String fmt)
          Creates a StyleDate with the current date/time and the given format, which it localizes.
StyleDate(StyleDate s)
          Creates a StyleDate with the same date/time, format, locale, and timezone as the one given.
 
Method Summary
 void addDays(int n)
          Ajust the day forward the given number of days, negative to go back.
 void addMillis(long m)
           
 void addMonths(int n)
          Ajust the month forward the given number of months, negative to go back.
 void addYears(int n)
          Ajust the year forward the given number of years, negative to go back.
 boolean after(java.util.Date d)
          Is this date after the argument?
 boolean after(StyleDate d)
          Is this date after the argument?
 java.util.Date asDate()
          Return the current date/time as a Date.
 long asMillis()
          Returns the current date/time in milliseconds.
 boolean before(java.util.Date d)
          Is this date before the argument?
 boolean before(StyleDate d)
          Is this date before the argument?
 java.lang.String format()
          Format the currently-set date/time.
 int get(int field)
          Gets an arbitrary Calendar field.
 int getFirstDayOfWeek()
          Returns the first day of the week for the locale.
static void main(java.lang.String[] args)
           
 void set(int field, int value)
          Sets an arbitrary Calendar field.
 void setDate(java.util.Date d)
          Sets the date/time to the given Date.
 void setDate(java.lang.String date)
          Sets the date/time to the formatted date, parsing leniently according to the current format.
 void setDay(int d)
          Sets the day of the month.
 void setDayOfWeek(int d)
          Sets the day of the week to the given constant within the current week, where the current week is determined by the locale's notion of "first day" of a week.
 void setFormat(java.lang.String fmt)
          Sets the format (does not localize it, whatever that implies).
 void setLocale(java.util.Locale loc)
           
 void setLocalizedFormat(java.lang.String fmt)
          Sets the format, translating it to a localized version.
 void setMillis(long m)
          Sets the date/time to an absolute value in milliseconds.
 void setMonth(int m)
          Sets the month, zero-based counting.
 void setTimeZone(java.util.TimeZone z)
           
 void setToStartOfDay()
          Sets the date/time to the beginning of the day, local time.
 void setYear(int y)
          Sets the year.
 java.lang.String sqlFormat()
          Format the currently-set date/time for SQL.
 java.lang.String toString()
          Formats the currently-set date/time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StyleDate

public StyleDate()
Creates a StyleDate with the current date and time.

StyleDate

public StyleDate(java.util.Date d)
Creates a StyleDate for the given Date.

StyleDate

public StyleDate(long millis)
Creates a StyleDate for the given absolute time in milliseconds.

StyleDate

public StyleDate(java.lang.String fmt)
Creates a StyleDate with the current date/time and the given format, which it localizes.

StyleDate

public StyleDate(StyleDate s)
Creates a StyleDate with the same date/time, format, locale, and timezone as the one given.

StyleDate

public StyleDate(java.util.Date d,
                 java.lang.String fmt)
Create a StyleDate with a particular format and Date.
Method Detail

setDate

public void setDate(java.util.Date d)
Sets the date/time to the given Date.

setDate

public void setDate(java.lang.String date)
             throws java.text.ParseException
Sets the date/time to the formatted date, parsing leniently according to the current format.

setMillis

public void setMillis(long m)
Sets the date/time to an absolute value in milliseconds.

setFormat

public void setFormat(java.lang.String fmt)
Sets the format (does not localize it, whatever that implies). Just as if calling the SimpleDateFormat constructor with a format parameter.

setLocalizedFormat

public void setLocalizedFormat(java.lang.String fmt)
Sets the format, translating it to a localized version. This appears to translate some of the pattern characters from standard ones for the default format into the corresponding characters for the Formatter's locale.

setTimeZone

public void setTimeZone(java.util.TimeZone z)

setLocale

public void setLocale(java.util.Locale loc)

asDate

public java.util.Date asDate()
Return the current date/time as a Date.

asMillis

public long asMillis()
Returns the current date/time in milliseconds.

sqlFormat

public java.lang.String sqlFormat()
Format the currently-set date/time for SQL.

format

public java.lang.String format()
Format the currently-set date/time.

toString

public java.lang.String toString()
Formats the currently-set date/time.
Overrides:
toString in class java.lang.Object

setDay

public void setDay(int d)
Sets the day of the month. Modifies the effective time of the of the calendar to match.

getFirstDayOfWeek

public int getFirstDayOfWeek()
Returns the first day of the week for the locale.

setDayOfWeek

public void setDayOfWeek(int d)
Sets the day of the week to the given constant within the current week, where the current week is determined by the locale's notion of "first day" of a week. For example, setDayOfWeek(Calendar.SUNDAY) sets the date/time to Sunday of "this" week, today if the week begins on Sunday, otherwise last Sunday.

setToStartOfDay

public void setToStartOfDay()
Sets the date/time to the beginning of the day, local time.

setMonth

public void setMonth(int m)
Sets the month, zero-based counting.

setYear

public void setYear(int y)
Sets the year.

addDays

public void addDays(int n)
Ajust the day forward the given number of days, negative to go back.

addMonths

public void addMonths(int n)
Ajust the month forward the given number of months, negative to go back.

addYears

public void addYears(int n)
Ajust the year forward the given number of years, negative to go back.

addMillis

public void addMillis(long m)

get

public int get(int field)
Gets an arbitrary Calendar field. Useful field constants are:

set

public void set(int field,
                int value)
Sets an arbitrary Calendar field.

after

public boolean after(java.util.Date d)
Is this date after the argument?

after

public boolean after(StyleDate d)
Is this date after the argument?

before

public boolean before(java.util.Date d)
Is this date before the argument?

before

public boolean before(StyleDate d)
Is this date before the argument?

main

public static void main(java.lang.String[] args)