From a88028874b45b24aede2fe7c67b2a8014f334ae6 Mon Sep 17 00:00:00 2001 From: "thibault.faure" Date: Thu, 15 Jun 2023 20:05:04 +0200 Subject: [PATCH] BAEL-5600 Code for the Read Date in Java using Scanner article --- .../core-java-io-apis-3/README.md | 5 ++++ core-java-modules/core-java-io-apis-3/pom.xml | 15 ++++++++++ .../com/baeldung/scanner/DateScanner.java | 29 +++++++++++++++++++ .../baeldung/scanner/DateScannerUnitTest.java | 26 +++++++++++++++++ core-java-modules/pom.xml | 1 + 5 files changed, 76 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-3/README.md create mode 100644 core-java-modules/core-java-io-apis-3/pom.xml create mode 100644 core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java create mode 100644 core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java diff --git a/core-java-modules/core-java-io-apis-3/README.md b/core-java-modules/core-java-io-apis-3/README.md new file mode 100644 index 0000000000..95d8dffce6 --- /dev/null +++ b/core-java-modules/core-java-io-apis-3/README.md @@ -0,0 +1,5 @@ +## Core Java IO APIs + +This module contains articles about core Java input/output(IO) APIs. + +### Relevant Articles: diff --git a/core-java-modules/core-java-io-apis-3/pom.xml b/core-java-modules/core-java-io-apis-3/pom.xml new file mode 100644 index 0000000000..8b2431397b --- /dev/null +++ b/core-java-modules/core-java-io-apis-3/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + core-java-io-apis-3 + core-java-io-apis-3 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java b/core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java new file mode 100644 index 0000000000..a9f3ba820a --- /dev/null +++ b/core-java-modules/core-java-io-apis-3/src/main/java/com/baeldung/scanner/DateScanner.java @@ -0,0 +1,29 @@ +package com.baeldung.scanner; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Scanner; + +public class DateScanner { + + LocalDate scanToLocalDate(String input) { + try (Scanner scanner = new Scanner(input)) { + String dateString = scanner.next(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return LocalDate.parse(dateString, formatter); + } + } + + Date scanToDate(String input) throws ParseException { + try (Scanner scanner = new Scanner(input)) { + String dateString = scanner.next(); + DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + return formatter.parse(dateString); + } + } + +} diff --git a/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java b/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java new file mode 100644 index 0000000000..1ecd440d95 --- /dev/null +++ b/core-java-modules/core-java-io-apis-3/src/test/java/com/baeldung/scanner/DateScannerUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.scanner; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import org.junit.jupiter.api.Test; + +class DateScannerUnitTest { + + @Test + void whenScanToLocalDate_ThenCorrectLocalDate() { + String dateString = "2018-09-09"; + assertEquals(LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyy-MM-dd")), new DateScanner().scanToLocalDate(dateString)); + } + + @Test + void whenScanToDate_ThenCorrectDate() throws ParseException { + String dateString = "2018-09-09"; + assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse(dateString), new DateScanner().scanToDate(dateString)); + } + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 806882a84f..9144e16359 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -81,6 +81,7 @@ core-java-io-4 core-java-io-apis core-java-io-apis-2 + core-java-io-apis-3 core-java-io-conversions core-java-jar core-java-jndi