From 57ed00a134839fb50b2bce7f0224121e6a0c3351 Mon Sep 17 00:00:00 2001 From: Dmytro Kulaiev Date: Fri, 24 Jun 2016 16:41:41 +0300 Subject: [PATCH] Code examples for Java 8 date migration (#460) --- .../baeldung/date_migration/Conversion.java | 19 ++++++ .../com/baeldung/date_migration/NewApi.java | 54 +++++++++++++++ .../com/baeldung/date_migration/OldApi.java | 68 +++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 core-java-8/src/main/java/com/baeldung/date_migration/Conversion.java create mode 100644 core-java-8/src/main/java/com/baeldung/date_migration/NewApi.java create mode 100644 core-java-8/src/main/java/com/baeldung/date_migration/OldApi.java diff --git a/core-java-8/src/main/java/com/baeldung/date_migration/Conversion.java b/core-java-8/src/main/java/com/baeldung/date_migration/Conversion.java new file mode 100644 index 0000000000..c672b7ee44 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/date_migration/Conversion.java @@ -0,0 +1,19 @@ +package com.baeldung.date_migration; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +public class Conversion { + public static void main(String[] args) { + Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant(); + ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime(); + Date dateFromInstant = Date.from(Instant.now()); + GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now()); + Instant instantFromDate = new Date().toInstant(); + ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId(); + } +} diff --git a/core-java-8/src/main/java/com/baeldung/date_migration/NewApi.java b/core-java-8/src/main/java/com/baeldung/date_migration/NewApi.java new file mode 100644 index 0000000000..0abf7f7864 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/date_migration/NewApi.java @@ -0,0 +1,54 @@ +package com.baeldung.date_migration; + +import java.text.ParseException; +import java.time.*; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; + +public class NewApi { + public void currentTime() { + ZonedDateTime now = ZonedDateTime.now(); + } + + public void specificTime() { + LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15); + } + + public void extractMonth() { + Month month = LocalDateTime.now().getMonth(); + } + + public void subtractTime() { + LocalDateTime fiveHoursBefore = LocalDateTime.now().minusHours(5); + } + + public void alterField() { + LocalDateTime inJune = LocalDateTime.now().withMonth(Month.JUNE.getValue()); + } + + public void truncate() { + LocalTime truncated = LocalTime.now().truncatedTo(ChronoUnit.HOURS); + } + + public void convertTimeZone() { + ZonedDateTime centralEastern = LocalDateTime.now().atZone(ZoneId.of("CET")); + } + + public void getTimeSpan() { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime hourLater = LocalDateTime.now().plusHours(1); + Duration span = Duration.between(now, hourLater); + } + + public void formatAndParse() throws ParseException { + LocalDate now = LocalDate.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + String formattedDate = now.format(formatter); + LocalDate parsedDate = LocalDate.parse(formattedDate, formatter); + } + + public void daysInMonth() { + int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth(); + } + +} diff --git a/core-java-8/src/main/java/com/baeldung/date_migration/OldApi.java b/core-java-8/src/main/java/com/baeldung/date_migration/OldApi.java new file mode 100644 index 0000000000..de4edf99e9 --- /dev/null +++ b/core-java-8/src/main/java/com/baeldung/date_migration/OldApi.java @@ -0,0 +1,68 @@ +package com.baeldung.date_migration; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +public class OldApi { + public void currentTime() { + Date now = new Date(); + } + + public void specificTime () { + Date birthDay = new GregorianCalendar(1990, Calendar.DECEMBER, 15).getTime(); + } + + public void extractMonth() { + int month = new GregorianCalendar().get(Calendar.MONTH); + } + + public void subtractTime() { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.add(Calendar.HOUR_OF_DAY, -5); + Date fiveHoursBefore = calendar.getTime(); + } + + public void alterField() { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.set(Calendar.MONTH, Calendar.JUNE); + Date inJune = calendar.getTime(); + } + + public void truncate() { + Calendar now = Calendar.getInstance(); + now.set(Calendar.MINUTE, 0); + now.set(Calendar.SECOND, 0); + now.set(Calendar.MILLISECOND, 0); + Date truncated = now.getTime(); + } + + public void convertTimeZone() { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.setTimeZone(TimeZone.getTimeZone("CET")); + Date centralEastern = calendar.getTime(); + } + + public void getTimeSpan() { + GregorianCalendar calendar = new GregorianCalendar(); + Date now = new Date(); + calendar.add(Calendar.HOUR, 1); + Date hourLater = calendar.getTime(); + long elapsed = hourLater.getTime() - now.getTime(); + } + + public void formatAndParse() throws ParseException { + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date now = new Date(); + String formattedDate = dateFormat.format(now); + Date parsedDate = dateFormat.parse(formattedDate); + } + + public void daysInMonth() { + Calendar calendar = new GregorianCalendar(1990, Calendar.FEBRUARY, 20); + int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); + } +}