Date parsing using Java 8
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 20 May 2015 00:03:06 +0000 (02:03 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 20 May 2015 00:03:06 +0000 (02:03 +0200)
Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateFormatUtils.java [new file with mode: 0644]
Allgemeines/DateParsingJava8/src/de/example/date/parsing/DateHourFormatter.java [new file with mode: 0644]
Allgemeines/DateParsingJava8/src/de/example/date/parsing/MainDateFormat.java [new file with mode: 0644]

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 (file)
index 0000000..334a216
--- /dev/null
@@ -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 (file)
index 0000000..6dfb391
--- /dev/null
@@ -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 (file)
index 0000000..6abb385
--- /dev/null
@@ -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");
+       }
+
+}