Merge pull request #14243 from thibaultfaure/article/BAEL-5600

BAEL-5600 Code for the Read Date in Java using Scanner article
This commit is contained in:
davidmartinezbarua 2023-06-19 13:07:37 -03:00 committed by GitHub
commit 33b4f08269
5 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,5 @@
## Core Java IO APIs
This module contains articles about core Java input/output(IO) APIs.
### Relevant Articles:

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-io-apis-3</artifactId>
<name>core-java-io-apis-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

@ -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);
}
}
}

View File

@ -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));
}
}

View File

@ -81,6 +81,7 @@
<module>core-java-io-4</module> <module>core-java-io-4</module>
<module>core-java-io-apis</module> <module>core-java-io-apis</module>
<module>core-java-io-apis-2</module> <module>core-java-io-apis-2</module>
<module>core-java-io-apis-3</module>
<module>core-java-io-conversions</module> <module>core-java-io-conversions</module>
<module>core-java-jar</module> <module>core-java-jar</module>
<module>core-java-jndi</module> <module>core-java-jndi</module>