Article View: aus.computers.java
Article #419Re: Formatted date/time via environment variable?
From: Paul Murray
Date: Mon, 09 Oct 2000 00:00
Date: Mon, 09 Oct 2000 00:00
127 lines
6395 bytes
6395 bytes
"Doug Rickard" <rickard@primus.com.au> wrote in message
news:39e107db@news.iprimus.com.au...
> variable, or a class that will return a formatted date/time string given a
> simple format definition string.
If all you want to do is convert dates which you *already know* are going to
be in 'DD-MMM-YYYY' format (eg, in a flat text file), then what you need is
a java.text.SimpleDateFormat().
DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
Date end_date = df.parse("12-JAN-1987");
If you have something more complicated in mind:
Java has three related classes that do date/time stuff.
java.util.Date. This class encapsulates the idea of a moment in time. A date
is like a number. The only operations you can do on a date is compare one to
another for greaterthan/lessthan/equalto. It is an "opaque" class - it's
actual contents are NOYB, however, most jvm implementations will represent
dates as a number of miliseconds since 1-Jan-1970 GMT.
java.util.Calendar. This class encapsulates the idea of dividing time up
into named intervals (days, months, eras, lunar cycles etc). A calendar is a
thing that says "that point of time is during a Tuesday in March". One
could, in theory, make up a calendar for mars or for the sidreal year
(rather than the solar) and plug it into the existing date/time
infrastructure. Try that with windows!
java.text.DateFormat. This class encapulates the idea of using a text string
to specify a Date.
Oh - and other classes of interest are java.util.Locale, which represents a
geographical, political, or cultural region; and java.util.TimeZone, which
is a time zone (duh).
The locale in which a jvm is running is a system property. Java does not use
environment variables - it uses properties. These properties should have
been set up when the jvm was installed. You get the default Locale with
Locale.getDefault();
and the default TimeZone with
TimeZone.getDefault();
which are a static methods. There may be more than one Locale in a timezone
(eg: nth and sth america), and more than one Timezone in a Locale (eg, the
USA). For what you want you probably don't need these classes directly, but
if you need the user to indicate where they are, you can construct a
picklists from Locale.getAvailableLocales(), and TimeZone.getAvailableIDs().
This also handy for internationalising your app with resource bundles (a
Locale also includes the concept of a language, CA_EN vs. CA_FR).
The next thing you need is a Calendar. If you use
Calendar.getInstance(),
you'll get a calendar which is of the default type for your system locale,
initialised to the current time. In most places, the calendar you get back
will be an instance of GregorianCalendar. I suppose there is an
implementation of the Chinese calendar out there somewhere if you need it.
If you are giving your users a picklist of locales, you can use
Calendar.getInstance(Locale)
Calendar.getInstance(TimeZone)
Calendar.getInstance(TimeZone, Locale)
to get a calendar for the locale they chose. Of course, you can specifically
use a GregorianCalendar
Calendar cal = new GregorianCalendar();
Calendar cal = new GregorianCalendar(user_locale);
if you don't want to deal with the Year of the Rat, roman Ides and Calends,
or Jovian sidreal years (the day on Jupiter is 10 earth-hours long).
Calendars allow you to do things like adding one day to a date, or finding
the first weekday of next month. Some things are specific to certain types
of calendar - eg, finding the next year of the Rat. If you are dealing with
concepts like "month" and "day of the week", then you may need to admit that
you are assuming a Gregorian calendar, and explicitly construct one. It's
nothing to be ashamed of, everyone does it.
So. You have a localised calendar. Each calendar may have several ways of
specifying dates as text. To convert text strings to non-text values, you
use a java.Text.Format. There is a subclass of this, java.Text.DateFormat,
for handling dates.
To get a default date formatter for the current locale, use
java.text.DateFormat.getInstance();
java.text.DateFormat.getDateInstance();
java.text.DateFormat.getTimeInstance();
java.text.DateFormat.getDateTimeInstance();
as applicable
*** MOST OF THE TIME, THIS IS ALL YOU NEED TO DO ***
just call
formatter.parse(userInputString)
to convert a string to a date, or
formatter.format(employeeTerminationDate)
to convert a date to a string
'Xcuse the shouting, but that was for people just flicking through this
post. getDateInstance should correctly return dd/mm/yyyy, mm/dd/yyyy,
dd.mm.yyyy, yyyy.mm.dd etc for wherever the user happens to be when it is
run.
You can ask for a long, medium or short date/time by calling
DateFormat.getInstance(DateFormat.LONG)
DateFormat.getInstance(DateFormat.MEDIUM)
DateFormat.getInstance(DateFormat.SHORT)
DateFormat.getInstance(DateFormat.DEFAULT)
DateFormat.getInstance(DateFormat.FULL)
The meanings of which depend on the calendar and the locale.
And, of course, you can
DateFormat.getDateInstance(DateFormat.FULL, Locale.FRENCH)
For a long format french date format, and I presume
DateFormat df = new DateFormat();
df.setCalendar(new WiccanLunarCalendar());
to plan your next sabbat.
And this is as much as I know about the topic. It took me a while to figure
it out, but it's very very neat, really. Cheers!
--------------------------------------------------------------
Repent! Quit your job! Slack off!
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Technical Details
Message-ID:
<STgE5.66838$c5.184944@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39e107db@news.iprimus.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,e9a2d875ce6c6a1d X-Google-Attributes: gid10d6db,public From: "Paul Murray" <pmurray@nospam.bigpond.com> Subject: Re: Formatted date/time via environment variable? Date: 2000/10/09 Message-ID: <STgE5.66838$c5.184944@newsfeeds.bigpond.com> X-Deja-AN: 679252255 References: <39e107db@news.iprimus.com.au> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.3018.1300 X-Trace: newsfeeds.bigpond.com 971086450 139.134.125.89 (Mon, 09 Oct 2000 20:14:10 EST) Organization: Telstra BigPond Internet Services (http://www.bigpond.com) X-MSMail-Priority: Normal NNTP-Posting-Date: Mon, 09 Oct 2000 20:14:10 EST Newsgroups: aus.computers.java