Merge pull request #12935 from achraftt/BAEL-5791-adaptation
BAEL-5791: Convert String to Instant (creating new module)
This commit is contained in:
		
						commit
						1b48232d56
					
				
							
								
								
									
										6
									
								
								core-java-modules/core-java-datetime-string-2/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								core-java-modules/core-java-datetime-string-2/README.md
									
									
									
									
									
										Normal file
									
								
							| @ -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:  | ||||||
|  | 
 | ||||||
							
								
								
									
										36
									
								
								core-java-modules/core-java-datetime-string-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								core-java-modules/core-java-datetime-string-2/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | |||||||
|  | <?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> | ||||||
|  | 
 | ||||||
|  |     <build> | ||||||
|  |         <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> | ||||||
|  |         <maven.compiler.source>1.8</maven.compiler.source> | ||||||
|  |         <maven.compiler.target>1.8</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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user