diff --git a/core-java-modules/core-java-datetime-string-2/README.md b/core-java-modules/core-java-datetime-string-2/README.md new file mode 100644 index 0000000000..1b59d76993 --- /dev/null +++ b/core-java-modules/core-java-datetime-string-2/README.md @@ -0,0 +1,6 @@ +## Java Dates Parsing and Formatting Cookbooks and Examples + +This module contains articles about parsing and formatting Java date and time objects. + +### Relevant Articles: + diff --git a/core-java-modules/core-java-datetime-string-2/pom.xml b/core-java-modules/core-java-datetime-string-2/pom.xml new file mode 100644 index 0000000000..ac404b0a70 --- /dev/null +++ b/core-java-modules/core-java-datetime-string-2/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + core-java-datetime-string-2 + ${project.parent.version} + core-java-datetime-string-2 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + commons-validator + commons-validator + ${commons-validator.version} + + + + + core-java-datetime-string-2 + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 1.6 + 1.9 + 1.9 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-datetime-string-2/src/test/java/com/baeldung/instant/StringToInstantConverterUnitTest.java b/core-java-modules/core-java-datetime-string-2/src/test/java/com/baeldung/instant/StringToInstantConverterUnitTest.java new file mode 100644 index 0000000000..a3732bf7da --- /dev/null +++ b/core-java-modules/core-java-datetime-string-2/src/test/java/com/baeldung/instant/StringToInstantConverterUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.instant; + +import org.junit.Test; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Locale; + +import static org.assertj.core.api.Assertions.assertThat; + +public class StringToInstantConverterUnitTest { + String stringDate = "09:15:30 PM, Sun 10/09/2022"; + String pattern = "hh:mm:ss a, EEE M/d/uuuu"; + + @Test public void givenStringTimeStamp_whenConvertingWithInstantUsingTimeZone_thenConvertToInstant() { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.US); + LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateTimeFormatter); + ZoneId zoneId = ZoneId.of("America/Chicago"); + ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId); + Instant instant = zonedDateTime.toInstant(); + assertThat(instant.toString()).isEqualTo("2022-10-10T02:15:30Z"); + } + + @Test public void givenStringTimeStamp_whenConvertingWithLocalDateTime_thenConvertToInstant() { + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.US); + LocalDateTime localDateTime = LocalDateTime.parse(stringDate, dateTimeFormatter); + assertThat(localDateTime.toString()).isEqualTo("2022-10-09T21:15:30"); + } +} \ No newline at end of file