8
BPJ444: Dates Peter Liu Tim McKenna Seneca@York

BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

Embed Size (px)

DESCRIPTION

Date and Time ucurrent date and time (in milliseconds) after January 1, :00:00 GMT u long ms = System.currentTimeMillis( ) umilliseconds as a date & time object u java.util.Date date = new Date(); uDate is mostly deprecated because… uis a Date created at this moment here the same as one created in Australia?  same ms but different date/time to us uDate assumes local time zone!

Citation preview

Page 1: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

BPJ444: Dates Peter Liu

Tim McKennaSeneca@York

Page 2: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

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: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

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: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

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: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

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: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

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: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

Formatting Date and Time java.text.SimpleDateFormat

- constructors - date formatting

symbols - format( ) only works if set() was followed by

get() Example: FormatDateTime.java

Page 8: BPJ444: Dates Peter Liu Tim McKenna Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import

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