bb.util
Class Date2

java.lang.Object
  extended by java.util.Date
      extended by bb.util.Date2
All Implemented Interfaces:
Serializable, Cloneable, Comparable<Date>

public final class Date2
extends Date

A replacement for Date which corrects all its design mistakes. In particular, this class:

  1. is is immutable
  2. eliminates deprecated methods
  3. has a correct toString implementaion
Because this class extends Date, it is a drop in replacement for Date (provided deprecated methods are avoided; see below).

Immutability

This class achieves partial immutability by overriding every mutator method of Date to throw an
UnsupportedOperationException.

This type of immutability means that instances are safe to be directly returned from accessor methods without copying, used as keys in hash tables, used in Date object pools, etc because their state cannot be changed.

Unfortunately, this partial immutability is distinct from full immutability. The chief shortcoming is that this class cannot fix it's superclass's mistake of not making all of its fields final. This has some subtle implications for multithread safety.

Deprecated methods

Being a subclass of Date, this class cannot literally remove its deprecated methods, but what it does do is override them all to always throw an UnsupportedOperationException. This, at least, guarantees that users cannot use that bad API.

Multithread safety

A truly immutable class is always multithread safe. Unfortunately, this class's
partial immutability, due to its lack of final fields, means that it requires safe publication in order to be multithread safe. If you are not familiar with this concept, carefully read Java Concurrency in Practice by Goetz et al, especially Chapter 3. Once safely published, this class is fully multithread safe because of its partial immutability. Perhaps the most common way to safely publish instances of this class is to assign them to a data structure that is only reachable via a final field (so that field's finalness provides the necessary memory visibility ordering).

Author:
Brent Boyer
See Also:
Serialized Form

Field Summary
private static long serialVersionUID
           
 
Constructor Summary
Date2(Date date)
           
Date2(long time)
           
 
Method Summary
 int getDate()
           
 int getDay()
           
 int getHours()
           
 int getMinutes()
           
 int getMonth()
           
 int getSeconds()
           
 int getTimezoneOffset()
           
 int getYear()
           
static long parse(String s)
           
 void setDate(int date)
           
 void setHours(int hours)
           
 void setMinutes(int minutes)
           
 void setMonth(int month)
           
 void setSeconds(int seconds)
           
 void setTime(long time)
           
 void setYear(int year)
           
 String toGMTString()
           
 String toLocaleString()
           
 String toString()
          Returns an ISO 8601 formated description of all the date/time fields of this instance.
static long UTC(int year, int month, int date, int hrs, int min, int sec)
           
 
Methods inherited from class java.util.Date
after, before, clone, compareTo, equals, getTime, hashCode
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values
Constructor Detail

Date2

public Date2(Date date)

Date2

public Date2(long time)
Method Detail

getDate

public int getDate()
            throws UnsupportedOperationException
Overrides:
getDate in class Date
Throws:
UnsupportedOperationException

getDay

public int getDay()
           throws UnsupportedOperationException
Overrides:
getDay in class Date
Throws:
UnsupportedOperationException

getHours

public int getHours()
             throws UnsupportedOperationException
Overrides:
getHours in class Date
Throws:
UnsupportedOperationException

getMinutes

public int getMinutes()
               throws UnsupportedOperationException
Overrides:
getMinutes in class Date
Throws:
UnsupportedOperationException

getMonth

public int getMonth()
             throws UnsupportedOperationException
Overrides:
getMonth in class Date
Throws:
UnsupportedOperationException

getSeconds

public int getSeconds()
               throws UnsupportedOperationException
Overrides:
getSeconds in class Date
Throws:
UnsupportedOperationException

getTimezoneOffset

public int getTimezoneOffset()
                      throws UnsupportedOperationException
Overrides:
getTimezoneOffset in class Date
Throws:
UnsupportedOperationException

getYear

public int getYear()
            throws UnsupportedOperationException
Overrides:
getYear in class Date
Throws:
UnsupportedOperationException

setDate

public void setDate(int date)
             throws UnsupportedOperationException
Overrides:
setDate in class Date
Throws:
UnsupportedOperationException

setHours

public void setHours(int hours)
              throws UnsupportedOperationException
Overrides:
setHours in class Date
Throws:
UnsupportedOperationException

setMinutes

public void setMinutes(int minutes)
                throws UnsupportedOperationException
Overrides:
setMinutes in class Date
Throws:
UnsupportedOperationException

setMonth

public void setMonth(int month)
              throws UnsupportedOperationException
Overrides:
setMonth in class Date
Throws:
UnsupportedOperationException

setSeconds

public void setSeconds(int seconds)
                throws UnsupportedOperationException
Overrides:
setSeconds in class Date
Throws:
UnsupportedOperationException

setTime

public void setTime(long time)
             throws UnsupportedOperationException
Overrides:
setTime in class Date
Throws:
UnsupportedOperationException

setYear

public void setYear(int year)
             throws UnsupportedOperationException
Overrides:
setYear in class Date
Throws:
UnsupportedOperationException

parse

public static long parse(String s)
                  throws UnsupportedOperationException
Throws:
UnsupportedOperationException

toGMTString

public String toGMTString()
                   throws UnsupportedOperationException
Overrides:
toGMTString in class Date
Throws:
UnsupportedOperationException

toLocaleString

public String toLocaleString()
                      throws UnsupportedOperationException
Overrides:
toLocaleString in class Date
Throws:
UnsupportedOperationException

UTC

public static long UTC(int year,
                       int month,
                       int date,
                       int hrs,
                       int min,
                       int sec)
                throws UnsupportedOperationException
Throws:
UnsupportedOperationException

toString

public String toString()
Returns an ISO 8601 formated description of all the date/time fields of this instance.

Overrides:
toString in class Date