Dates and times

From Android Wiki

Revision as of 04:36, 30 January 2012 by Ldo (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, searcha

Java (and Android) offer a confusing plethora of different classes to do with dates and times. This page will try to shed some clarity on the thicket.

  • java.util.GregorianCalendar — this is the main class to be used for constructing and manipulating dates and times. Note that many important methods come from its java.util.Calendar superclass.
  • java.util.TimeZone — gives you information about the current system time zone as well as other available time zones that the system knows about.
  • java.util.Date — this remains an essential class, even though most of its constructors and methods are deprecated. You can obtain one of these from a Calendar object by calling its getTime method. The only reason you need objects of this class is because the formatting routines don’t work with Calendar objects!
  • java.text.DateFormat — this is where you will find the functions for doing locale-dependent date formatting. Note they only work on Date objects, not Calendar or GregorianCalendar objects.
  • android.text.format.DateFormat — convenience routines for obtaining suitable java.text.DateFormat objects for the user’s current locale/format settings. Also lets you format dates according to custom formats.
  • java.text.SimpleDateFormat — lets you create custom date formats.
Personal tools