public class TimeUtil extends Object
System.currentTimeMillis()
Modifier and Type | Field and Description |
---|---|
static long |
DAY
Number of milliseconds in an day.
|
static long |
HOUR
Number of milliseconds in an hour.
|
static long |
MINUTE
Number of milliseconds in a minute.
|
static long |
SECOND
Number of milliseconds in a second.
|
Modifier and Type | Method and Description |
---|---|
static long |
computeDailyTime(long currentTime,
LocalTime localTime,
ZoneId zoneId)
Calculating the daily time relative to the current timestamp, taking into account the time zone.
|
static int |
getMillisFromTime(long timeMillis)
Returns correct number of milliseconds with proper handling negative values.
|
static int |
getSecondsFromTime(long timeMillis)
Returns correct number of seconds with proper handling negative values and overflows.
|
static TimeZone |
getTimeZone(String tz)
Gets the
TimeZone for the given ID similar same as TimeZone.getTimeZone(String) but
throwing IllegalArgumentException if the time-zone cannot be parsed. |
static TimeZone |
getTimeZoneGmt()
Returns GMT time-zone.
|
static LocalTime |
parseLocalTime(String localTime)
Parse the string representing the local time in string format
DateTimeFormatter.ISO_LOCAL_TIME . |
public static final long SECOND
public static final long MINUTE
public static final long HOUR
public static final long DAY
public static int getSecondsFromTime(long timeMillis)
getSecondsFromTime(timeMillis) * 1000L + getMillisFromTime(timeMillis) == timeMillis
as as long the time in seconds fits into int.getMillisFromTime(long)
public static int getMillisFromTime(long timeMillis)
getSecondsFromTime(timeMillis) * 1000L + getMillisFromTime(timeMillis) == timeMillis
as as long the time in seconds fits into int.getSecondsFromTime(long)
public static TimeZone getTimeZoneGmt()
public static TimeZone getTimeZone(String tz)
TimeZone
for the given ID similar same as TimeZone.getTimeZone(String)
but
throwing IllegalArgumentException
if the time-zone cannot be parsed. Also note that shortened
custom IDs are not allowed, e.g. "GMT-8:00" instead of "GMT-08:00" would throw an exception.tz
- the ID for a TimeZone
, either an abbreviation
such as "PST", a full name such as "America/Los_Angeles", or a custom ID such as "GMT-08:00".
Note that the support of abbreviations is for compatibility only and full names should be used.TimeZone
.IllegalArgumentException
- if the given ID cannot be understood.public static long computeDailyTime(long currentTime, LocalTime localTime, ZoneId zoneId)
localTime
result will be localTime
in current day.
If the current time is more or equal than localTime
result will be localTime
in the next day in that time.
All results are calculated for the zoneId
time zone.
We have two special cases of season time shifting:
localTime
points to nonexistent time the nearest exist time in future will be chosencurrentTime
- current epoch millislocalTime
- desired daily time in LocalTime formatzoneId
- time zone for calculating daily timepublic static LocalTime parseLocalTime(String localTime)
DateTimeFormatter.ISO_LOCAL_TIME
. A null or empty string interpreted as null.localTime
- in string format.DateTimeParseException
- if the text cannot be parsedCopyright © 2002–2025 Devexperts LLC. All rights reserved.