public final class DateUtils extends Object
Date
) to the new (java.time
) JDK
Special thanks to http://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate#answer-27378709
which inspired this code
See also http://stackoverflow.com/questions/19431234/converting-between-java-time-localdatetime-and-java-util-dateModifier and Type | Field and Description |
---|---|
private static DateTimeFormatter |
__ISO_OFFSET_DATE_TIME
The ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30.000+01:00'.
|
Modifier | Constructor and Description |
---|---|
private |
DateUtils() |
Modifier and Type | Method and Description |
---|---|
static Date |
asDate(LocalDate localDate)
|
static Date |
asDate(LocalDateTime localDateTime)
Converts this
LocalDateTime object to a Date . |
static Date |
asDate(LocalDateTime localDateTime,
ZoneId zone)
Converts this
LocalDateTime object to a Date . |
static Date |
asDate(LocalDate localDate,
ZoneId zone)
|
static Date |
asDate(ZonedDateTime zonedDateTime)
Converts this
ZonedDateTime object to a Date . |
static Instant |
asInstant(Date date)
|
static LocalDate |
asLocalDate(Date date)
|
static LocalDate |
asLocalDate(Date date,
ZoneId zone)
|
static LocalDateTime |
asLocalDateTime(Date date)
Converts this
Date object to a LocalDateTime . |
static LocalDateTime |
asLocalDateTime(Date date,
ZoneId zone)
Converts this
Date object to a LocalDateTime . |
static ZonedDateTime |
asZonedDateTime(Date date,
ZoneId zone)
|
static String |
dateToString(Date value)
|
static String |
formatDuration(Duration duration)
Format a duration for logs
|
static String |
formatDuration(long duration)
Format a duration for logs
|
private static String |
formatNumber(long number,
int nbNumbers) |
static DateTimeFormatter |
getISODateTimeFormatter()
Get the ISO date-time formatter that formats or parses a date-time with an offset, such as '2011-12-03T10:15:30.000+01:00'.
|
static Date |
parse(String value)
Parses a String into a
Date , using ISO 8601 format. |
private static DateTimeFormatter __ISO_OFFSET_DATE_TIME
private DateUtils()
public static Instant asInstant(Date date)
date
- The date objectDate
objectpublic static ZonedDateTime asZonedDateTime(Date date, ZoneId zone)
date
- The date objectzone
- The zoneDate
objectpublic static LocalDate asLocalDate(Date date, ZoneId zone)
public static LocalDate asLocalDate(Date date)
public static LocalDateTime asLocalDateTime(Date date, ZoneId zone)
Date
object to a LocalDateTime
.
This returns a LocalDateTime
with the same year, month, day and time as this Date
.date
- The date objectzone
- The zoneLocalDateTime
part of this Date
public static LocalDateTime asLocalDateTime(Date date)
Date
object to a LocalDateTime
.
This returns a LocalDateTime
with the same year, month, day and time as this Date
.date
- The date objectLocalDateTime
part of this Date
public static Date asDate(LocalDateTime localDateTime)
LocalDateTime
object to a Date
.localDateTime
- The local date time objectDate
part of this LocalDateTime
public static Date asDate(LocalDateTime localDateTime, ZoneId zone)
LocalDateTime
object to a Date
.localDateTime
- The local date time objectzone
- The zoneDate
part of this LocalDateTime
public static Date asDate(ZonedDateTime zonedDateTime)
ZonedDateTime
object to a Date
.zonedDateTime
- The local date time objectDate
part of this LocalDateTime
public static String formatDuration(Duration duration)
duration
- duration to logpublic static String formatDuration(long duration)
duration
- miliseconds representing the durationprivate static String formatNumber(long number, int nbNumbers)
public static DateTimeFormatter getISODateTimeFormatter()
DateTimeFormatter.ISO_OFFSET_DATE_TIME
but force 3-digits milliseconds.public static String dateToString(Date value)
value
- the value to convertString