BAEL-5791: Convert String to Instant (creating new module)
This commit is contained in:
parent
24389cbb20
commit
03a4639341
|
@ -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:
|
||||||
|
|
|
@ -0,0 +1,58 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
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-datetime-string-2</artifactId>
|
||||||
|
<version>${project.parent.version}</version>
|
||||||
|
<name>core-java-datetime-string-2</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung.core-java-modules</groupId>
|
||||||
|
<artifactId>core-java-modules</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-validator</groupId>
|
||||||
|
<artifactId>commons-validator</artifactId>
|
||||||
|
<version>${commons-validator.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>core-java-datetime-string-2</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<commons-validator.version>1.6</commons-validator.version>
|
||||||
|
<maven.compiler.source>1.9</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.9</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue