This commit is related to the article BAEL-7232 (#15252)

This commit adds a new test class "TimeToLocalDateTimeUnitTest" that shows how to convert a long timestamp into a LocalDateTime object.
This commit is contained in:
MohamedHelmyKassab 2023-11-20 19:18:13 +02:00 committed by GitHub
parent c10a404f57
commit a3e0639a75
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.timestamptolocaldatetime;
import org.joda.time.DateTimeZone;
import org.junit.Test;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import static org.junit.Assert.assertEquals;
public class TimeToLocalDateTimeUnitTest {
private static final long timestampInMillis = 1700010123000L;
private static final String expectedTimestampString = "2023-11-15 01:02:03";
@Test
public void givenTimestamp_whenConvertingToLocalDateTime_thenConvertSuccessfully() {
Instant instant = Instant.ofEpochMilli(timestampInMillis);
LocalDateTime localDateTime =
LocalDateTime.ofInstant(instant, ZoneId.of("UTC"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = localDateTime.format(formatter);
assertEquals(expectedTimestampString, formattedDateTime);
}
@Test
public void givenJodaTime_whenGettingTimestamp_thenConvertToLong() {
DateTime dateTime = new DateTime(timestampInMillis, DateTimeZone.UTC);
org.joda.time.LocalDateTime localDateTime = dateTime.toLocalDateTime();
org.joda.time.format.DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
String actualTimestamp = formatter.print(localDateTime);
assertEquals(expectedTimestampString, actualTimestamp);
}
}