From 3c8a85f2b1ad217bf8f7ce7ce01b37799219ec1a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 20 May 2015 02:03:06 +0200 Subject: [PATCH] Date parsing using Java 8 --- .../de/example/date/parsing/DateFormatUtils.java | 38 ++++++++++++++++++++++ .../de/example/date/parsing/DateHourFormatter.java | 24 ++++++++++++++ .../de/example/date/parsing/MainDateFormat.java | 37 +++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateFormatUtils.java create mode 100644 Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateHourFormatter.java create mode 100644 Allgemeines/DateParsingJava8/src/de/example/date/parsing/MainDateFormat.java diff --git a/Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateFormatUtils.java b/Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateFormatUtils.java new file mode 100644 index 0000000..334a216 --- /dev/null +++ b/Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateFormatUtils.java @@ -0,0 +1,38 @@ +package de.example.date.parsing; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Date; + + +public class DateFormatUtils { + + public static String format(Date date, String format) { + final ZoneId timeZone = ZoneId.of("America/Los_Angeles"); + + return DateFormatUtils.format(date, format, timeZone); + } + + public static String format(Date date, String format, ZoneId timeZone) { + final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); + final Instant instantDate = Instant.ofEpochMilli(date.getTime()); + final LocalDateTime localDateTime = LocalDateTime.ofInstant(instantDate, timeZone); + + return localDateTime.format(formatter); + } + + public static Date parse(String text, String format) { + final ZoneId timeZone = ZoneId.of("America/Los_Angeles"); + + return DateFormatUtils.parse(text, format, timeZone); + } + + public static Date parse(String text, String format, ZoneId timeZone) { + final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format); + final LocalDateTime dateTimeJava8 = LocalDateTime.parse(text, formatter); + + return Date.from(dateTimeJava8.atZone(timeZone).toInstant()); + } +} diff --git a/Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateHourFormatter.java b/Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateHourFormatter.java new file mode 100644 index 0000000..6dfb391 --- /dev/null +++ b/Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateHourFormatter.java @@ -0,0 +1,24 @@ +package de.example.date.parsing; + +import java.util.Date; + + +public class DateHourFormatter { + + public static final String DATE_FORMAT = "dd/MM/yyyy HH:mm"; + + private DateHourFormatter() { + } + + public static String formatToDateTime(Date date) { + if (date == null) { + return ""; + } + return DateFormatUtils.format(date, DATE_FORMAT); + } + + public static Date parseToDate(String text) { + return DateFormatUtils.parse(text, DATE_FORMAT); + } + +} diff --git a/Allgemeines/DateParsingJava8/src/de/example/date/parsing/MainDateFormat.java b/Allgemeines/DateParsingJava8/src/de/example/date/parsing/MainDateFormat.java new file mode 100644 index 0000000..6abb385 --- /dev/null +++ b/Allgemeines/DateParsingJava8/src/de/example/date/parsing/MainDateFormat.java @@ -0,0 +1,37 @@ +package de.example.date.parsing; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.Date; + + +public class MainDateFormat { + + public static void main(String[] args) { + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"); + ZoneId timeZone = ZoneId.of("America/Los_Angeles"); + + // Just date without minutes and seconds. + LocalDate dateJava8 = LocalDate.parse("12/05/2014 18:40", formatter); + Date date = Date.from(dateJava8.atStartOfDay().atZone(timeZone).toInstant()); + + // Date with minutes and seconds. + LocalDateTime dateTimeJava8 = LocalDateTime.parse("12/05/2014 18:40", formatter); + Date dateTime = Date.from(dateTimeJava8.atZone(timeZone).toInstant()); + + Date now = new Date(); + Instant instant = Instant.ofEpochMilli(now.getTime()); + LocalDateTime currentLocalDateTime = LocalDateTime.ofInstant(instant, timeZone); + String currentTime = currentLocalDateTime.format(formatter); + + String dateHourCurrentTimeFormatter = DateHourFormatter.formatToDateTime(now); + + + Date dateCurrentTimeFormatter = DateHourFormatter.parseToDate("12/05/2014 18:40"); + } + +} -- 2.1.4