Java Utililty Methods ZonedDateTime Calculate

List of utility methods to do ZonedDateTime Calculate

Description

The list of methods to do ZonedDateTime Calculate are organized into topic(s).

Method

ZonedDateTimeaddOffset(ZonedDateTime time, String offset)
Applies an ISO 8601 Duration to a ZonedDateTime .
final int tPos = offset.indexOf('T');
final String periodOffset;
final String durationOffset;
switch (tPos) {
case -1:
    periodOffset = offset;
    break;
case 1:
...
ZonedDateTimeaddRandomDeltaDays(ZonedDateTime ZDT, int minDays, int maxDays, int minHour, int maxHour)
Adds/substract randomly between minDays and maxDays number of days to this date and sets the hours within the range prescribed (min/maxHours), and a randomly selected minute between 0 and 60.
if (ZDT == null)
    return null;
int days = (int) Math.floor((Math.random() * (maxDays - minDays)) + minDays);
int hours = (int) Math.floor((Math.random() * (maxHour - minHour)) + minHour);
int minutes = (int) Math.floor((Math.random() * 60));
return ZDT.plusDays(days).withHour(hours).withMinute(minutes);
ZonedDateTimeaddRandomDeltaMinutes(ZonedDateTime ZDT, int minMinutes, int maxMinutes)
Adds/substract randomly between min/maxMinutes number of minutes to this date.
if (ZDT == null)
    return null;
int minutes = (int) Math.round((Math.random() * (minMinutes - minMinutes)) + minMinutes);
return ZDT.plusMinutes(minutes);
ZonedDateTimeasZonedDateTime(Date date)
Calls #asZonedDateTime(Date,ZoneId) with the system default time zone.
return asZonedDateTime(date, ZoneId.systemDefault());
ZonedDateTimecalculateTimeForSunrise(ZonedDateTime from, ZonedDateTime to)
calculate Time For Sunrise
Duration d = Duration.between(from, to);
long distance = d.getSeconds() / 60 / 60 / 2;
return from.plusHours(distance);
StringcanonicalTimeString(ZonedDateTime time)
ISO8601 can map the same date and time to many different string representations.
ZonedDateTime asUTC = time.withZoneSameInstant(ZoneOffset.UTC);
return asUTC.format(canonicalFormatter);
ZonedDateTimecloneZonedDateTime(ZonedDateTime zonedDateTime)
clone Zoned Date Time
if (zonedDateTime == null) {
    return null;
return zonedDateTime.withZoneSameInstant(METZONE);
intcomputeDays(ZonedDateTime Start, ZonedDateTime End)
Comput ethe number of days using th emidnight rule, i.e., from 23:59 Monday night to 00:01 Tuesday morning, that counts as one day.
int days = (int) ChronoUnit.DAYS.between(Start, End);
if (secondsSinceMidnight(Start) > secondsSinceMidnight(End))
    ++days;
return days;
ZonedDateTimeconvertDateToZonedDateTime(Date date, String timezone)
convert Date To Zoned Date Time
if (timezone == null || timezone.equals("")) {
    return date.toInstant().atZone(ZoneId.systemDefault());
return date.toInstant().atZone(ZoneId.of(timezone));
DateconvertForPersistence(@Nullable ZonedDateTime dt)
Convert the given ZonedDateTime to a UTC date for persistence
if (dt == null)
    return null;
ZonedDateTime atUtc = dt.withZoneSameInstant(ZoneOffset.UTC);
return new Date(atUtc.toInstant().toEpochMilli());