KEMBAR78
Java dates | PPTX
Java Dates
Sujit Kumar
Zenolocity LLC
Copyright @
java.util.Date
• Represents an instant in time
• Signed long representing number of millisecs
since epoch
• Epoch based. Epoch is Jan 01, 1970 00:00:00
GMT
• Date() constructor creates a date object
representing the current time
• Date(long) constructor creates a date object
representing that time.
System.currentTimeMillis() returns the current
epoch based time.
Date Methods
Comparison Methods:
• after(Date)
• before(Date)
• compareTo(Date)
• equals(Date)
Note: Lot of deprecated methods in Date. Use
Calendar class and it’s methods instead.
java.sql.Date
• Java.util.Date is a basic, all-purpose Date object. It
simply stores a Date (as a long) and allows you to
display it.
java.sql.Date extends java.util.Date to add the
following functionality:
1) toString outputs the date as "yyyy-mm-dd" instead
of as a Locale specific String.
2) add the valueOf method to read a String of "yyyymm-dd" format and parse it into a java.sql.Date object.
java.util.Calendar
• Represents a specific instance in time
• Abstract class – it’s concrete derived classes
support different calendar styles.
• Gregorian Calendar most common
implementation
• Calendar.getInstance() returns a localized
Calendar object initialized with current date
and time.
Calendar (contd…)
• set and get each of the date and time fields
like
month, day, year, hour, minutes, seconds, etc.
• Compare calendar objects using before, after
and compareTo.
• Date and time arithmetic with the add
method.
• Use getTime() to convert to a Date object.
java.util.TimeZone
• Represents a timezone offset, calculates the daylight
savings time as well.
• TimeZone.getTimeZone() method returns a TimeZone
object based on the system’s time zone setting.
• TimeZone.getTimeZone("America/Los_Angeles") to get
the TimeZone object for a specific timezone ID.
• Invoke the getTimeZone() method on a calendar object
to return the corresponding TimeZone object.
• Important methods: getId(), getDisplayName() and
getRawOffset()
Format and Parse Dates
• java.text.DateFormat : abstract class
• The format(Date) method generates a String
representation of a Date object
• The parse(String) method generates a Date
object by parsing a date/time string
• Java.text.SimpleDateFormat – concrete class
for user defined patterns for formatting &
parsing dates and times.
Thread Safe SimpleDateFormat
•

SimpleDateFormat is not thread safe.

•

Here is an implementation to make it thread safe.

public class ThreadSafeSimpleDateFormat {
private DateFormat df;
public ThreadSafeSimpleDateFormat(String format) {
this.df = new SimpleDateFormat(format);
}
public synchronized String format(Date date) {
return df.format(date);
}

}

public synchronized Date parse(String string) throws ParseException {
return df.parse(string);
}
DateFormat Static Methods
• Static methods to obtain date/time formatters.
• Based on the default or a specific locale.
• Examples:
DateFormat.getDateTimeInstance()
DateFormat.getDateInstance(
DateFormat.LONG, Locale.GERMAN)
DateFormat.getTimeInstance(DateFormat.LONG)
Joda-time
• High quality replacement for the
Java date and time classes.
• Design allows for
multiple calendar systems, while still providing a
simple API. The 'default' calendar is the
ISO8601 standard which is used by XML.
• The Gregorian, Julian, Buddhist, Coptic, Ethiopic
and Islamic systems are also included.
• Supporting classes include time
zone, duration, format and parsing.
• http://joda-time.sourceforge.net/

Java dates

  • 1.
  • 2.
    java.util.Date • Represents aninstant in time • Signed long representing number of millisecs since epoch • Epoch based. Epoch is Jan 01, 1970 00:00:00 GMT • Date() constructor creates a date object representing the current time • Date(long) constructor creates a date object representing that time. System.currentTimeMillis() returns the current epoch based time.
  • 3.
    Date Methods Comparison Methods: •after(Date) • before(Date) • compareTo(Date) • equals(Date) Note: Lot of deprecated methods in Date. Use Calendar class and it’s methods instead.
  • 4.
    java.sql.Date • Java.util.Date isa basic, all-purpose Date object. It simply stores a Date (as a long) and allows you to display it. java.sql.Date extends java.util.Date to add the following functionality: 1) toString outputs the date as "yyyy-mm-dd" instead of as a Locale specific String. 2) add the valueOf method to read a String of "yyyymm-dd" format and parse it into a java.sql.Date object.
  • 5.
    java.util.Calendar • Represents aspecific instance in time • Abstract class – it’s concrete derived classes support different calendar styles. • Gregorian Calendar most common implementation • Calendar.getInstance() returns a localized Calendar object initialized with current date and time.
  • 6.
    Calendar (contd…) • setand get each of the date and time fields like month, day, year, hour, minutes, seconds, etc. • Compare calendar objects using before, after and compareTo. • Date and time arithmetic with the add method. • Use getTime() to convert to a Date object.
  • 7.
    java.util.TimeZone • Represents atimezone offset, calculates the daylight savings time as well. • TimeZone.getTimeZone() method returns a TimeZone object based on the system’s time zone setting. • TimeZone.getTimeZone("America/Los_Angeles") to get the TimeZone object for a specific timezone ID. • Invoke the getTimeZone() method on a calendar object to return the corresponding TimeZone object. • Important methods: getId(), getDisplayName() and getRawOffset()
  • 8.
    Format and ParseDates • java.text.DateFormat : abstract class • The format(Date) method generates a String representation of a Date object • The parse(String) method generates a Date object by parsing a date/time string • Java.text.SimpleDateFormat – concrete class for user defined patterns for formatting & parsing dates and times.
  • 9.
    Thread Safe SimpleDateFormat • SimpleDateFormatis not thread safe. • Here is an implementation to make it thread safe. public class ThreadSafeSimpleDateFormat { private DateFormat df; public ThreadSafeSimpleDateFormat(String format) { this.df = new SimpleDateFormat(format); } public synchronized String format(Date date) { return df.format(date); } } public synchronized Date parse(String string) throws ParseException { return df.parse(string); }
  • 10.
    DateFormat Static Methods •Static methods to obtain date/time formatters. • Based on the default or a specific locale. • Examples: DateFormat.getDateTimeInstance() DateFormat.getDateInstance( DateFormat.LONG, Locale.GERMAN) DateFormat.getTimeInstance(DateFormat.LONG)
  • 11.
    Joda-time • High qualityreplacement for the Java date and time classes. • Design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. • The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included. • Supporting classes include time zone, duration, format and parsing. • http://joda-time.sourceforge.net/