Package java.time in Java 8 and later implement JSR-310.
Additional functionality is available from library ThreeTen [https://www.threeten.org/].
Maven dependency at [https://mvnrepository.com/artifact/org.threeten/threeten-extra].
Javadoc > https://www.threeten.org/threeten-extra/apidocs/org.threeten.extra/org/threeten/extra/Interval.html
An interval represents the time on the time-line between two Instants. The class stores the start and end instants, with the start inclusive and the end exclusive.
Application time helper, for converting XMLGregorianCalendar, etc.
package edu.cou.myapp.helper;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.springframework.lang.Nullable;
import lombok.experimental.UtilityClass;
/**
* Time helper (including Date, etc.)
*/
@UtilityClass
public class AppTimeHelper {
/** 'Europe/Madrid' time zone id */
private static final ZoneId TIME_ZONE_ID_EUROPE_MADRID = ZoneId.of("Europe/Madrid");
/**
* XMLGregorianCalendar to String [yyyy-MM-dd HH:mm:ss]
*
* @param calendar
* @return -
*/
public static String toYyyymmddHhmmss(XMLGregorianCalendar calendar) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime = LocalDateTime.of(calendar.getYear(), calendar.getMonth(), calendar
.getDay(), calendar.getHour(), calendar.getMinute(), calendar.getSecond());
return localDateTime.format(formatter);
}
/**
* @param xmlgc -
* @return A LocalDate of xmlgc in the Europe/Madrid time zone
*/
@Nullable
public static LocalDate toLocalDate(@Nullable XMLGregorianCalendar xmlgc) {
if (xmlgc == null) {
return null;
}
LocalDate ret;
if (xmlgc.getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
ret = xmlgc.toGregorianCalendar().toZonedDateTime().toLocalDate();
} else {
// Ensure we're working in the 'Europe/Madrid' time zone.
ret = xmlgc.toGregorianCalendar().toZonedDateTime().withZoneSameInstant(
TIME_ZONE_ID_EUROPE_MADRID).toLocalDate();
}
return ret;
}
/**
* @param xmlgc -
* @return A LocalDateTime of xmlgc in the Europe/Madrid time zone
*/
@Nullable
public static LocalDateTime toLocalDateTime(@Nullable XMLGregorianCalendar xmlgc) {
if (xmlgc == null) {
return null;
}
LocalDateTime ret;
if (xmlgc.getTimezone() == DatatypeConstants.FIELD_UNDEFINED) {
ret = xmlgc.toGregorianCalendar().toZonedDateTime().toLocalDateTime();
} else {
// Ensure we're working in the 'Europe/Madrid' time zone.
ret = xmlgc.toGregorianCalendar().toZonedDateTime().withZoneSameInstant(
TIME_ZONE_ID_EUROPE_MADRID).toLocalDateTime();
}
return ret;
}
/**
* LocalDateTime to XMLGregorianCalendar
*
* @param ldt
* @return -
* @throws DatatypeConfigurationException -
*/
public static XMLGregorianCalendar fromLocalDateTimeToXmlGregorianCalendar(LocalDateTime ldt)
throws DatatypeConfigurationException {
// Eg: "2020-12-31T23:59:59"
String lexicalRepresentation = ldt.format(DateTimeFormatter.ISO_DATE_TIME);
// Created without timezone
XMLGregorianCalendar ret = DatatypeFactory.newInstance().newXMLGregorianCalendar(
lexicalRepresentation);
return ret;
}
}
Use case: Given a local time (eg: 15:30:59), let's obtain the proper ZonedDateTime for a specific date (eg: 2020-08-20) and geographic region (eg: "Europe/Madrid").
Expected result:
The local time 15:30:59 in a winter time date must be in the UTC+1 time-zone; and in a summer time date must be in the UTC+2 time-zone.
Sample code:
LocalTime lt = LocalTime.of(15, 30, 59);
System.out.println("Local time = " + lt);
LocalDateTime ldtWinter = lt.atDate(LocalDate.of(2020, 2, 10));
LocalDateTime ldtSummer = lt.atDate(LocalDate.of(2020, 8, 20));
System.out.println("\n");
System.out.println("Datetime February = " + ldtWinter);
System.out.println("Datetime August = " + ldtSummer);
ZonedDateTime zdtWinter = ldtWinter.atZone(ZoneId.of("Europe/Madrid"));
ZonedDateTime zdtSummer = ldtSummer.atZone(ZoneId.of("Europe/Madrid"));
System.out.println("\n");
System.out.println("zdtWinter = " + zdtWinter);
System.out.println("zdtSummer = " + zdtSummer);
Sample output:
Note that the same local time becomes UTC+1 on Winter but UTC+2 on a Summer daylight saving date.
Local time = 15:30:59
Datetime February = 2020-02-10T15:30:59
Datetime August = 2020-08-20T15:30:59
zdtWinter = 2020-02-10T15:30:59+01:00[Europe/Madrid]
zdtSummer = 2020-08-20T15:30:59+02:00[Europe/Madrid]