Locale and Date Classes - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Locale and Date Classes

Description:

... called locale-sensitive and uses the Locale to tailor information for the user. ... contains static Format Fields that make it easy to get the information ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 15
Provided by: gae5
Category:

less

Transcript and Presenter's Notes

Title: Locale and Date Classes


1
Locale and Date Classes
  • Locale
  • Date
  • DateFormat
  • Gregorian Calendar

2
Locale Class
  • A Locale object represents a specific
    geographical, political, or cultural region. An
    operation that requires a Locale to perform its
    task is called locale-sensitive and uses the
    Locale to tailor information for the user.
  • The Locale Class contains a number of static
    constants to identify common locales. Because
    they are static, you can use them with the Class
    name (no instantiating a Locale object needed).
  • example Locale.CANADA
  • See the Sun Web Site documentation about the
    Locale class for a list of static constants you
    can use to represent countries/locales.
  • http//java.sun.com/j2se/1.4/docs/api/index.html

3
GregorianCalendar Class
  • GregorianCalendar is a concrete subclass of
    Calendar and provides the standard calendar used
    by most of the world.
  • The standard (Gregorian) calendar has 2 eras, BC
    and AD.
  • This Class contains static Format Fields that
    make it easy to get the information you want
    about a date/time. There are static Constants,
    too.
  • The GregorianCalendar Class also contains useful
    methods such as isLeapYear()

4
GregorianCalendar Fields
  • static Format Fields
  • AM_PM - 0 (AM) or 1 (PM)
  • DAY_OF_WEEK - 1 to 7
  • DAY_OF_YEAR - 1 to 366
  • MONTH - 0 to 11
  • DAY_OF_MONTH - 1 to 31
  • WEEK_OF_MONTH - 1 to 6
  • HOUR_OF_DAY - 0 to 23
  • HOUR - 1 to 12
  • MINUTE - 0 to 59
  • SECOND - 0 to 59
  • MILLISECOND - 0 to 999
  • YEAR - current year (e.g. 2002)

5
GregorianCalendar Constants
  • an example of static Constants
  • AM
  • APRIL
  • AUGUST
  • DECEMBER
  • FEBRUARY
  • FRIDAY
  • JANUARY
  • JULY
  • JUNE
  • MARCH
  • MAY
  • MONDAY
  • Etc.

6
  • //new calendar object using system date and time
  • GregorianCalander cal new GregorianCalendar( )
  • //get the day of the week (1SUNDAY, etc)
  • int day cal.get(cal.DAY_OF_WEEK)
  • //create a String of Date and Time
  • String dtg cal.get(cal.YEAR)
  • (cal.get(cal.MONTH) 1)
  • cal.get(cal.DAY_OF_MONTH)
  • cal.get(cal.HOUR_OF_DAY)
  • cal.get(cal.MINUTE)
  • cal.get(cal.SECOND)

7
  • //Instantiate new calendar object using a
    specific date
  • GregorianCalander cal
  • new GregorianCalendar( 2002,

  • GregorianCalendar.AUGUST,
  • 10 )
  • //Use this to create a Date object with this same
    date
  • Date myBirthday cal.getTime( )

8
The Date Classes
  • Date Class
  • defines an instant in time to the nearest
    millisecond measured from January 1, 1970 GMT
  • DateFormat Class
  • abstract class used to create meaningful String
    representations of Date objects

9
Date
  • Constructors ( only two )
  • Date( )
  • Creates an object based on the current time from
    your computer clock
  • Date( long time )
  • Creates an object based on the time value
  • time value must be in milliseconds since 1/1/70
    000000
  • Examples
  • Instantiate a new Date using the current system
    date and time
  • Date today new Date( )
  • Instantiate a new Date using a specific date
  • GregorianCalendar cal new GregorianCalendar(
    1962,

  • GregorianCalendar.AUGUST, 01 )
  • Date myBirthDay cal.getTime( )

10
DateFormat
  • Abstract class that provides format and locale
    templates for producing Dates and Times in
    readable String representations.
  • Formats
  • SHORT - numeric representation such as 01/01/01
    or 220
  • MEDIUM - 10-Jan-01
  • LONG - January 10, 2001
  • FULL - comprehensive representation such as
  • Wednesday, January 10, 2001 or 22014 GMT

11
DateFormat static methods
  • Default formatters
  • getDateInstance( ),
  • getTimeInstance( ),
  • getDateTimeInstance( )
  • Specify formatter
  • getDateInstance(int dateStyle),
  • getTimeInstance(int timeStyle),
    getDateTimeInstance(int dateStyle, int timeStyle)

12
Using Dates
  • //create a Date Object
  • Date today new Date( ) //system date and time
  • //get a DateFormat ( use Date/Time Instance
    default )
  • DateFormat fmtDateFormat.getDateTimeInstance()
  • // pass the Date through it
  • String defaultFmt fmt.format(today)
  • ____________________________________________
  • //get a DateFormat, specifying the style. . .
  • fmt DateFormat.getDateTimeInstance(DateFormat.FU
    LL)
  • String fullFmt fmt.format(today)

13
Lab
  • Date Formatting example on page 458-459 of text
    book.

14
Project 5
  • Create a class called Friend that contains
    String first name and last name attributes, and a
    Date birthdate attribute. Include an overloaded
    constructor that takes the first name, last name,
    birthyear, birthmonth, and birthday as parameters
    and sets the private attributes of your Friend.
    Include setters and getters for first and last
    name and the Date birthdate attribute.
  • Write a driver program that creates a Vector of 5
    of your friends initialize their names and
    birthday information, then display each friends
    birthday in the following Date and Locale
    formats (see example program on page 458-459)
  • FULL, LONG, SHORT, MEDIUM and US, UK, GERMANY,
    FRANCE
  • Hint Go to http//java.sun.com/j2se/1.4/api/inde
    x.html for help with using the Utility classes,
    their constructors, formatters, iterators, etc
Write a Comment
User Comments (0)
About PowerShow.com