public class Time
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
DATE_FORMAT_ISO8601 |
static java.lang.String |
DATE_FORMAT_ISO8601_NO_MILLIS |
static java.lang.String |
DATE_FORMAT_OF_DATE_TOSTRING |
static java.lang.String |
DATE_FORMAT_PREFERRED |
static java.lang.String |
DATE_FORMAT_PREFERRED_W_TZ |
static java.lang.String |
DATE_FORMAT_SIMPLE_STAMP |
static java.lang.String |
DATE_FORMAT_STAMP |
static long |
MILLIS_IN_DAY |
static long |
MILLIS_IN_HOUR |
static long |
MILLIS_IN_MINUTE |
static long |
MILLIS_IN_SECOND |
static long |
MILLIS_IN_YEAR |
static java.util.TimeZone |
TIME_ZONE_UTC
GMT/UTC/Z time zone constant
|
Constructor and Description |
---|
Time() |
Modifier and Type | Method and Description |
---|---|
static java.util.Date |
dropMilliseconds(java.util.Date date)
removes milliseconds from the date object; needed if serializing to ISO-8601 format
and want to serialize back and get the same data
|
static Duration |
elapsedSince(long timestamp)
returns the duration elapsed since the given timestamp (UTC)
|
static com.google.common.base.Function<Duration,java.lang.String> |
fromDurationToTimeStringRounded() |
static com.google.common.base.Function<java.lang.Long,java.lang.String> |
fromLongToTimeStringExact() |
static com.google.common.base.Function<java.lang.Long,java.lang.String> |
fromLongToTimeStringRounded() |
static long |
getTimeOfDayFromUtc(long timeUtc)
Calculates the number of milliseconds past midnight for a given UTC time.
|
static long |
getTimeUtc(java.util.TimeZone zone,
int year,
int month,
int date,
int hourOfDay,
int minute,
int second,
int millis)
Calculates the number of milliseconds past epoch for a given UTC time.
|
static java.util.TimeZone |
getTimeZone(java.lang.String code) |
static Maybe<java.lang.String> |
getTimeZoneOffsetString(java.lang.String tz,
int year,
int month,
int day)
convert a TimeZone e.g.
|
static java.lang.String |
getTimeZoneOffsetString(java.util.TimeZone tz,
int year,
int month,
int day)
as
getTimeZoneOffsetString(String, int, int, int) where the TimeZone is already instantiated |
static boolean |
hasElapsedSince(long timestamp,
Duration duration)
true iff it has been longer than the given duration since the given timestamp
|
static java.lang.String |
makeDateSimpleStampString()
returns the current time in "yyyy-MM-dd-HHmm" format,
suitable for machines to read but easier for humans too,
like
makeDateStampString() but not as precise |
static java.lang.String |
makeDateSimpleStampString(long date)
returns the time in "yyyy-MM-dd-HHmm" format, given a long (e.g.
|
static java.lang.String |
makeDateStampString()
returns the current time in "yyyyMMdd-HHmmssSSS" format,
suitable for machines to read with only numbers and dashes and quite precise (ms)
|
static java.lang.String |
makeDateStampString(long date)
returns the time in "yyyyMMdd-HHmmssSSS" format, given a long (e.g.
|
static java.lang.String |
makeDateString()
as
makeDateString(Date) for current date/time |
static java.lang.String |
makeDateString(java.util.Calendar date)
|
static java.lang.String |
makeDateString(java.util.Calendar date,
java.lang.String format)
as
makeDateString(Date, String, TimeZone) for the time zone of the given calendar object |
static java.lang.String |
makeDateString(java.util.Date date)
returns the time in "yyyy-MM-dd HH:mm:ss.SSS" format for the given date;
this format is numeric big-endian but otherwise optimized for people to read, with spaces and colons and dots;
time is local to the server and time zone is not included
|
static java.lang.String |
makeDateString(java.util.Date date,
java.lang.String format)
as
makeDateString(Date, String, TimeZone) for the local time zone |
static java.lang.String |
makeDateString(java.util.Date date,
java.lang.String format,
java.util.TimeZone tz)
as
makeDateString(Date, String, TimeZone) for the given time zone; consider TimeZone#GMT |
static java.lang.String |
makeDateString(long date)
as
makeDateString(Date) for long millis since UTC epock |
static java.lang.String |
makeTimeString(long t,
boolean round)
given an elapsed time, makes it readable, eg 44d 6h, or 8s 923ms, optionally rounding
|
static java.lang.String |
makeTimeStringExact(Duration d) |
static java.lang.String |
makeTimeStringExact(long t) |
static java.lang.String |
makeTimeStringExact(long t,
java.util.concurrent.TimeUnit unit) |
static java.lang.String |
makeTimeStringNano(long tn,
boolean round) |
static java.lang.String |
makeTimeStringNanoExact(long tn) |
static java.lang.String |
makeTimeStringNanoRounded(long tn) |
static java.lang.String |
makeTimeStringRounded(Duration d) |
static java.lang.String |
makeTimeStringRounded(long t) |
static java.lang.String |
makeTimeStringRounded(long t,
java.util.concurrent.TimeUnit unit) |
static java.lang.String |
makeTimeStringRounded(com.google.common.base.Stopwatch timer) |
static java.lang.String |
makeTimeStringRoundedSince(long utc) |
static java.util.Calendar |
newCalendarFromDate(java.util.Date date) |
static java.util.Calendar |
newCalendarFromMillisSinceEpochUtc(long timestamp) |
static long |
now()
more readable and shorter convenience for System.currentTimeMillis()
|
static java.util.Calendar |
parseCalendar(java.lang.String input)
Parses dates from string, accepting many formats including ISO-8601 and http://yaml.org/type/timestamp.html,
e.g.
|
static Maybe<java.util.Calendar> |
parseCalendarFormat(java.lang.String dateString,
java.text.DateFormat format) |
static Maybe<java.util.Calendar> |
parseCalendarFormat(java.lang.String dateString,
java.lang.String format) |
static Maybe<java.util.Calendar> |
parseCalendarMaybe(java.lang.String input)
as
parseCalendar(String) but returning a Maybe rather than throwing or returning null |
static java.util.Date |
parseDate(java.lang.String input)
As
parseCalendar(String) but returning a Date ,
(i.e. |
static java.util.Date |
parseDateString(java.lang.String dateString,
java.text.DateFormat format)
Deprecated.
since 0.7.0 use
parseCalendar(String) for general or parseCalendarFormat(String, DateFormat) for a format,
plus parseCalendarUtc(String) if you want to accept UTC |
static Duration |
parseDuration(java.lang.String timeString)
Convenience for
Duration.parse(String) . |
static long |
parseElapsedTime(java.lang.String timeString)
|
static double |
parseElapsedTimeAsDouble(java.lang.String timeString)
Parses a string eg '5s' or '20m 22.123ms', returning the number of milliseconds it represents;
-1 on blank or never or off or false.
|
static long |
parseTimeString(java.lang.String timeString)
Deprecated.
since 0.7.0 see
parseElapsedTime(String) |
static double |
parseTimeStringAsDouble(java.lang.String timeString)
Deprecated.
since 0.7.0 see
parseElapsedTimeAsDouble(String) |
static long |
roundFromMillis(long millis,
long millisPerUnit) |
static long |
roundFromMillis(long millis,
java.util.concurrent.TimeUnit units) |
static void |
sleep(Duration duration)
as
sleep(long) |
static void |
sleep(long millis)
sleep which propagates Interrupted as unchecked
|
static long |
timeRemaining(long startTime,
long maxTime)
Calculates how long until maxTime has passed since the given startTime.
|
static com.google.common.base.Function<java.lang.Long,java.lang.String> |
toDateStampString() |
static com.google.common.base.Function<java.lang.Long,java.lang.String> |
toDateString() |
static com.google.common.base.Function<java.lang.Long,java.lang.String> |
toTimeString()
Deprecated.
since 0.7.0 use
fromLongToTimeStringExact() |
static com.google.common.base.Function<java.lang.Long,java.lang.String> |
toTimeStringRounded()
Deprecated.
since 0.7.0 use
fromLongToTimeStringRounded() |
public static final java.lang.String DATE_FORMAT_PREFERRED_W_TZ
public static final java.lang.String DATE_FORMAT_PREFERRED
public static final java.lang.String DATE_FORMAT_STAMP
public static final java.lang.String DATE_FORMAT_SIMPLE_STAMP
public static final java.lang.String DATE_FORMAT_OF_DATE_TOSTRING
public static final java.lang.String DATE_FORMAT_ISO8601
public static final java.lang.String DATE_FORMAT_ISO8601_NO_MILLIS
public static final long MILLIS_IN_SECOND
public static final long MILLIS_IN_MINUTE
public static final long MILLIS_IN_HOUR
public static final long MILLIS_IN_DAY
public static final long MILLIS_IN_YEAR
public static final java.util.TimeZone TIME_ZONE_UTC
public static java.lang.String makeDateString()
makeDateString(Date)
for current date/timepublic static java.lang.String makeDateString(long date)
makeDateString(Date)
for long millis since UTC epockpublic static java.lang.String makeDateString(java.util.Date date)
public static java.lang.String makeDateString(java.util.Date date, java.lang.String format)
makeDateString(Date, String, TimeZone)
for the local time zonepublic static java.lang.String makeDateString(java.util.Date date, java.lang.String format, java.util.TimeZone tz)
makeDateString(Date, String, TimeZone)
for the given time zone; consider TimeZone#GMT
public static java.lang.String makeDateString(java.util.Calendar date)
public static java.lang.String makeDateString(java.util.Calendar date, java.lang.String format)
makeDateString(Date, String, TimeZone)
for the time zone of the given calendar objectpublic static com.google.common.base.Function<java.lang.Long,java.lang.String> toDateString()
public static java.lang.String makeDateStampString()
public static java.lang.String makeDateStampString(long date)
makeDateStampString()
public static java.lang.String makeDateSimpleStampString()
makeDateStampString()
but not as precisepublic static java.lang.String makeDateSimpleStampString(long date)
makeDateSimpleStampString()
public static com.google.common.base.Function<java.lang.Long,java.lang.String> toDateStampString()
public static java.lang.String makeTimeStringExact(long t, java.util.concurrent.TimeUnit unit)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringRounded(long t, java.util.concurrent.TimeUnit unit)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringRounded(com.google.common.base.Stopwatch timer)
public static java.lang.String makeTimeStringExact(long t)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringRounded(long t)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringRoundedSince(long utc)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringExact(Duration d)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringRounded(Duration d)
makeTimeString(long, boolean)
public static java.lang.String makeTimeString(long t, boolean round)
public static java.lang.String makeTimeStringNanoExact(long tn)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringNanoRounded(long tn)
makeTimeString(long, boolean)
public static java.lang.String makeTimeStringNano(long tn, boolean round)
makeTimeString(long, boolean)
public static com.google.common.base.Function<java.lang.Long,java.lang.String> fromLongToTimeStringExact()
@Deprecated public static com.google.common.base.Function<java.lang.Long,java.lang.String> toTimeString()
fromLongToTimeStringExact()
public static com.google.common.base.Function<java.lang.Long,java.lang.String> fromLongToTimeStringRounded()
@Deprecated public static com.google.common.base.Function<java.lang.Long,java.lang.String> toTimeStringRounded()
fromLongToTimeStringRounded()
public static com.google.common.base.Function<Duration,java.lang.String> fromDurationToTimeStringRounded()
public static void sleep(long millis)
public static void sleep(Duration duration)
sleep(long)
public static long getTimeOfDayFromUtc(long timeUtc)
public static long getTimeUtc(java.util.TimeZone zone, int year, int month, int date, int hourOfDay, int minute, int second, int millis)
public static long roundFromMillis(long millis, java.util.concurrent.TimeUnit units)
public static long roundFromMillis(long millis, long millisPerUnit)
public static long timeRemaining(long startTime, long maxTime)
public static Duration parseDuration(java.lang.String timeString)
Duration.parse(String)
.public static long parseElapsedTime(java.lang.String timeString)
parseElapsedTimeAsDouble(String)
. Consider using parseDuration(String)
for a more usable return type.java.lang.NumberFormatException
- if cannot be parsed (or if null)@Deprecated public static long parseTimeString(java.lang.String timeString)
parseElapsedTime(String)
@Deprecated public static double parseTimeStringAsDouble(java.lang.String timeString)
parseElapsedTimeAsDouble(String)
public static double parseElapsedTimeAsDouble(java.lang.String timeString)
java.lang.NumberFormatException
- if cannot be parsed (or if null)public static java.util.Calendar newCalendarFromMillisSinceEpochUtc(long timestamp)
public static java.util.Calendar newCalendarFromDate(java.util.Date date)
public static java.util.Date parseDate(java.lang.String input)
parseCalendar(String)
but returning a Date
,
(i.e. a record where the time zone has been applied and forgotten).public static java.util.Calendar parseCalendar(java.lang.String input)
Millis since epoch (1970) is also supported to represent the epoch (0) or dates in this millenium, but to prevent ambiguity of e.g. "20150615", any other dates prior to the year 2001 are not accepted. (However if a type Long is supplied, e.g. from a YAML parse, it will always be treated as millis since epoch.)
Other formats including locale-specific variants, e.g. recognising month names, are supported but this may vary from platform to platform and may change between versions.
public static Maybe<java.util.Calendar> parseCalendarMaybe(java.lang.String input)
parseCalendar(String)
but returning a Maybe
rather than throwing or returning nullpublic static java.util.TimeZone getTimeZone(java.lang.String code)
public static Maybe<java.lang.String> getTimeZoneOffsetString(java.lang.String tz, int year, int month, int day)
public static java.lang.String getTimeZoneOffsetString(java.util.TimeZone tz, int year, int month, int day)
getTimeZoneOffsetString(String, int, int, int)
where the TimeZone
is already instantiatedpublic static java.util.Date parseDateString(java.lang.String dateString, java.text.DateFormat format)
parseCalendar(String)
for general or parseCalendarFormat(String, DateFormat)
for a format,
plus parseCalendarUtc(String)
if you want to accept UTCIf no time zone supplied, this defaults to the TZ configured at the brooklyn server.
public static Maybe<java.util.Calendar> parseCalendarFormat(java.lang.String dateString, java.lang.String format)
public static Maybe<java.util.Calendar> parseCalendarFormat(java.lang.String dateString, java.text.DateFormat format)
public static java.util.Date dropMilliseconds(java.util.Date date)
public static Duration elapsedSince(long timestamp)
public static boolean hasElapsedSince(long timestamp, Duration duration)
public static long now()