BAEL-5600 Code for the Read Date in Java using Scanner article
This commit is contained in:
parent
fec5f3af14
commit
a88028874b
|
@ -0,0 +1,5 @@
|
|||
## Core Java IO APIs
|
||||
|
||||
This module contains articles about core Java input/output(IO) APIs.
|
||||
|
||||
### Relevant Articles:
|
|
@ -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>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
|
@ -81,6 +81,7 @@
|
|||
<module>core-java-io-4</module>
|
||||
<module>core-java-io-apis</module>
|
||||
<module>core-java-io-apis-2</module>
|
||||
<module>core-java-io-apis-3</module>
|
||||
<module>core-java-io-conversions</module>
|
||||
<module>core-java-jar</module>
|
||||
<module>core-java-jndi</module>
|
||||
|
|
Loading…
Reference in New Issue