Class Time
- Since:
- 4.0.5
- Author:
- Andrew Cowie
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
formatTime
(String format, long when) Output the date per the descriptor given in theformat
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.
-
Method Details
-
setTimeZone
Adjust the timezone being used for formatted time/date output calculations.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 callingtzset()
. 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 thatstrftime()
thinks it is in without doing this, then please let us know.- Parameters:
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.- See Also:
-
formatTime
Output the date per the descriptor given in theformat
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 theTZ
environment variable, which in turn draws from the system zoneinfo libraries, data which is much more up to date than what Java offers.- Parameters:
when
- the number of seconds since Epoch being the date/time group you wish to present according toformat
.- See Also:
-
makeTime
public 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. Takes into account the current system timezone setting.This is essentially a wrapper around
mktime()
.- Parameters:
year
- the year, four digitsmonth
- the month, range1-12
day
- the day, range1-{28,29,30,31}
(depending on the month, of course)hour
- the hour, range0-23
minute
- the minute,0-59
second
- the second,0-59
- See Also:
-