public class Time
extends Object
Modifier and Type | Method and Description |
---|---|
static String |
formatTime(String format,
long when)
Output the date per the descriptor given in the
format
parameter. |
static long |
makeTime(int year,
int month,
int day,
int hour,
int minute,
int second)
Compose a timestamp (the number of seconds since epoch) from individual
components of a date/time group.
|
static void |
setTimeZone(String zoneinfo)
Adjust the timezone being used for formatted time/date output
calculations.
|
public static String formatTime(String format, long when)
format
parameter.
Feel free to call this several times to get isolated Strings with just the bits you want. For example, we do
long when; when = System.currentTimeMillis(); ... = formatTime("%H:%M", when); ... = formatTime("%a,%e %b %y", when); ... = formatTime("%Z", when);in quick succession to get time, date, and timezone, respectively, just the way we like it.
This is a wrapper around strftime()
from the standard C
library. It is exposed because the formatting is done according to the
value of the TZ
environment variable, which in turn draws
from the system zoneinfo libraries, data which is much more up
to date than what Java offers.
when
- the number of seconds since Epoch being the date/time group
you wish to present according to format
.public static long makeTime(int year, int month, int day, int hour, int minute, int second)
This is essentially a wrapper around mktime()
.
year
- the year, four digitsmonth
- the month, range 1-12
day
- the day, range 1-{28,29,30,31}
(depending on
the month, of course)hour
- the hour, range 0-23
minute
- the minute, 0-59
second
- the second, 0-59
public static void setTimeZone(String zoneinfo)
This will change the timezone as far as the entire program is
concerned. If you have some reason to restore the original setting,
use getEnv()
.
This works by changing the environment variable TZ
and
then calling tzset()
. That may or may not be what you
want. We've made it static since this impacts the entire
process. If someone can figure out a way to change the timezone that
strftime()
thinks it is in without doing this, then please
let us know.
zoneinfo
- A String of the form "Australia/Sydney", "America/Toronto",
"America/New_York", or "Europe/London", etc. Some zones have
definitive abbreviations, notably Universal Time,
Co-ordinated as "UTC". In case it wasn't obvious, these are
files in /usr/share/zoneinfo/
and are relative
to that path.