Update StringToOffsetDateTimeUnitTest.java (#16433)
This commit is contained in:
parent
52d390e8ea
commit
9e74ea8ea1
|
@ -3,6 +3,7 @@ package com.baeldung.stringtooffsetdatetime;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
@ -14,17 +15,18 @@ public class StringToOffsetDateTimeUnitTest {
|
||||||
public void givenDateTimeString_whenUsingOffsetDateTimeParse_thenConvertToOffsetDateTime() {
|
public void givenDateTimeString_whenUsingOffsetDateTimeParse_thenConvertToOffsetDateTime() {
|
||||||
OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateTimeString);
|
OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateTimeString);
|
||||||
|
|
||||||
OffsetDateTime expected = OffsetDateTime.of(2024, 4, 11, 10, 15, 30, 0, OffsetDateTime.parse(dateTimeString).getOffset());
|
OffsetDateTime expected = OffsetDateTime.of(2024, 4, 11, 10, 15, 30, 0, ZoneOffset.ofHours(1));
|
||||||
assertEquals(expected, offsetDateTime);
|
assertEquals(expected, offsetDateTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDateTimeStringAndFormatter_whenUsingDateTimeFormatter_thenConvertToOffsetDateTime() {
|
public void givenDateTimeStringAndFormatter_whenUsingDateTimeFormatter_thenConvertToOffsetDateTime() {
|
||||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");
|
String customDateTimeString = "11-04-2024 10:15:30 +0100";
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss Z");
|
||||||
|
|
||||||
OffsetDateTime offsetDateTime = OffsetDateTime.parse(dateTimeString, formatter);
|
OffsetDateTime offsetDateTime = OffsetDateTime.parse(customDateTimeString, formatter);
|
||||||
|
|
||||||
OffsetDateTime expected = OffsetDateTime.of(2024, 4, 11, 10, 15, 30, 0, OffsetDateTime.parse(dateTimeString).getOffset());
|
OffsetDateTime expected = OffsetDateTime.of(2024, 4, 11, 10, 15, 30, 0, ZoneOffset.ofHours(1));
|
||||||
assertEquals(expected, offsetDateTime);
|
assertEquals(expected, offsetDateTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue