Download presentation
Presentation is loading. Please wait.
Published byWalter Hodges Modified over 9 years ago
1
BPJ444: Dates Peter Liu Tim McKenna Seneca@York
2
Dates, Calendars, and what year is this? §Java tries to take an OOD approach to “when is now?” §import java.util.*; // get util package §Date class – represents a point in time §Calendar class – a general way to organize time §java.GregorianCalendar –a specific way to organize time.specific way to organize time
3
Date and Time ucurrent date and time (in milliseconds) after January 1, 1970 00: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!
4
GregorianCalendar uGregorianCalendar extends Calendar ucaptures ms timestamp and time zone u constructors can create current or other date u getTime( ) returns a Date object u get(Calendar.YEAR / MONTH / DATE / etc ) u set or add(Calendar.YEAR/etc, int ) u boolean gc1.after(gc2) or gc1.before(gc2) uExample: DateTime.java
5
GregorianCalendar caution §always follow one or more.set(Calendar.XXX) method calls with any.get(Calendar.XXX) call. §GregorianCalendar today = new GregorianCalendar(); // current date today.set(Calendar.HOUR_OF_DAY, 0); today.set(Calendar.MINUTE, 0); today.set(Calendar.SECOND, 0); today.get(Calendar.DATE); // update fields
6
GregorianCalendar alternative §see Joda-Time at http://joda-time.sourceforge.net/index.html http://joda-time.sourceforge.net/index.html §Stephen Colebourne is the Lead Technical Architect … see his presentation in a PDFStephen ColebournePDF §Joda-Time may be needed in applications that do a lot of date and time processing
7
Formatting Date and Time ujava.text.SimpleDateFormat - constructors - date formatting symbols - format( ) uonly works if set() was followed by get() uExample: FormatDateTime.java
8
Preset Formatters ujava.text.DateFormat - the parent class of SimpleDateFormat uDateFormat constants: SHORT, MEDIUM, LONG, FULL uDateFormat static methods : getDateInstance( ), getTimeInstance( ), getDateTimeInstance( ) retrieve normal format for that system uExample: DefaultFormats.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.