9
JAC444: Dates Tim McKenna Seneca@York

JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

Embed Size (px)

Citation preview

Page 1: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

JAC444: Dates

Tim McKennaSeneca@York

Page 2: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

Dates, Calendars, and what year is this?

Java tries to take an OOD approach to“when is now?”

import java.util.*; // get util packageDate class – represents a point in timeCalendar class – a general way to

organize timejava.GregorianCalendar –a

specific way to organize time.

Page 3: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

Date and Time current date and time (in milliseconds)

after January 1, 1970 00:00:00 GMT long ms = System.currentTimeMillis( )

milliseconds as a date & time object java.util.Date date = new Date();

Date is mostly deprecated because… is a Date created at this moment here

the same as one created in Australia? same ms but different date/time to us

Date assumes local time zone!

Page 4: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

GregorianCalendar GregorianCalendar extends Calendar captures ms timestamp and time zone

constructors can create current or other date getTime( ) returns a Date object get(Calendar.YEAR / MONTH / DATE / etc ) set or add(Calendar.YEAR/etc , int ) boolean gc1.after(gc2) or gc1.before(gc2)

Example: DateTime.java

Page 5: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

GregorianCalendar cautionalways follow one or more

.set(Calendar.XXX) method calls with any .get(Calendar.XXX) call.

GregorianCalendar today = new GregorianCalendar(); // current datetoday.set(Calendar.HOUR_OF_DAY, 0);today.set(Calendar.MINUTE, 0);today.set(Calendar.SECOND, 0);today.get(Calendar.DATE); // update fields

Page 6: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

GregorianCalendar alternative

see Joda-Time at http://joda-time.sourceforge.net/index.html

Stephen Colebourne is the Lead Technical Architect … see his presentation in a PDF

Joda-Time may be needed in applications that do a lot of date and time processing

Page 7: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

GregorianCalendar utilities

Databrew in the bobjects package offers:daysInterval()

number of days difference between two local dates ignoring time of day.

timeInterval() – "days hh:mm:ss" difference calendarInterval() – "yy-mm-dd hh:mm:ss"

difference

Page 8: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

Formatting Date and Time

java.text.SimpleDateFormat- constructors

- date formatting symbols - format( )

only works if set() was followed by get()

Example: FormatDateTime.java

Page 9: JAC444: Dates Tim McKenna Seneca@York. Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*;

Preset Formatters

java.text.DateFormat - the parent class of SimpleDateFormat

DateFormat constants: SHORT, MEDIUM, LONG, FULL

DateFormat static methods : getDateInstance( ),

getTimeInstance( ), getDateTimeInstance( )retrieve normal format for that system

Example: DefaultFormats.java