From 08e902cb26072d8959bbcc191a91d1737d235c7d Mon Sep 17 00:00:00 2001 From: Dmytro Korniienko Date: Thu, 4 Oct 2018 23:14:59 +0300 Subject: [PATCH] BAEL-2258 Guide to DateTimeFormatter. parse() method usage tests added --- .../DateTimeFormatterUnitTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java index 1260a6db92..36a5f6210b 100644 --- a/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java +++ b/core-java-8/src/test/java/com/baeldung/internationalization/DateTimeFormatterUnitTest.java @@ -9,6 +9,7 @@ import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.time.format.FormatStyle; import java.time.temporal.ChronoUnit; import java.util.Locale; @@ -119,4 +120,39 @@ public class DateTimeFormatterUnitTest { Assert.assertEquals("2018-03-09-03:00", DateTimeFormatter.ISO_OFFSET_DATE.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); Assert.assertEquals("Fri, 9 Mar 2018 00:00:00 -0300", DateTimeFormatter.RFC_1123_DATE_TIME.format(LocalDate.of(2018, 3, 9).atStartOfDay(ZoneId.of("UTC-3")))); } + + @Test + public void shouldParseDateTime() { + Assert.assertEquals(LocalDate.of(2018, 3, 12), LocalDate.from(DateTimeFormatter.ISO_LOCAL_DATE.parse("2018-03-09")).plusDays(3)); + } + + @Test + public void shouldParseFormatStyleFull() { + ZonedDateTime dateTime = ZonedDateTime.from(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).parse("Tuesday, August 23, 2016 1:12:45 PM EET")); + Assert.assertEquals(ZonedDateTime.of(LocalDateTime.of(2016, 8, 23, 22, 12, 45), ZoneId.of("Europe/Bucharest")), dateTime.plusHours(9)); + } + + @Test + public void shouldParseDateWithCustomFormatter() { + DateTimeFormatter europeanDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); + Assert.assertFalse(LocalDate.from(europeanDateFormatter.parse("15.08.2014")).isLeapYear()); + } + + @Test + public void shouldParseTimeWithCustomFormatter() { + DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a"); + Assert.assertTrue(LocalTime.from(timeFormatter.parse("12:25:30 AM")).isBefore(LocalTime.NOON)); + } + + @Test + public void shouldParseZonedDateTimeWithCustomFormatter() { + DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z"); + Assert.assertEquals(7200, ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15 GMT+02:00")).getOffset().getTotalSeconds()); + } + + @Test(expected = DateTimeParseException.class) + public void shouldExpectAnExceptionIfDateTimeStringNotMatchPattern() { + DateTimeFormatter zonedFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm z"); + ZonedDateTime.from(zonedFormatter.parse("31.07.2016 14:15")); + } }