BAEL-5928: Adds conversion b/w Epoch & LocalDateTime

This commit is contained in:
abdul0214 2023-07-19 22:57:13 +02:00
parent 6f78670338
commit 2743bcc498
4 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package com.baeldung.epochconversion;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class EpochToLocalDate {
public static void main(String[] args) {
long millisSinceEpoch = 2131242L;
ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); // or: ZoneId.systemDefault();
// to get All Zone Ids available, we can use the function below
// Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
LocalDate date =
Instant.ofEpochMilli(millisSinceEpoch)
.atZone(zoneId)
.toLocalDate();
LocalDateTime time =
Instant.ofEpochMilli(millisSinceEpoch)
.atZone(zoneId)
.toLocalDateTime();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.epochconversion;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class LocalDateToEpoch {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.of("Europe/Amsterdam"); // or: ZoneId.systemDefault()
LocalDate date = LocalDate.now();
long epochMilliSecondsAtDate = date.atStartOfDay(zoneId).toInstant().toEpochMilli();
// epoch for time
LocalDateTime time = LocalDateTime.parse("2019-11-15T13:15:30");
long epochMilliSecondsAtTime = time.atZone(zoneId).toInstant().toEpochMilli();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.epochconversion;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EpochToLocalDateUnitTest {
@Test
void givenEpoch_thenComputeLocalDateCorrectly() {
ZoneId zoneId = ZoneId.systemDefault();
LocalDate expectedDate = LocalDate.of(1995, 4, 15);
long date = expectedDate.atStartOfDay(zoneId).toInstant().toEpochMilli();
LocalDate actualDate =
Instant.ofEpochMilli(date)
.atZone(zoneId)
.toLocalDate();
assertEquals(expectedDate, actualDate);
}
@Test
void givenEpoch_thenComputeLocalDateTimeCorrectly() {
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime expectedTime = LocalDateTime.parse("2019-11-15T13:15:30");
long time = expectedTime.atZone(zoneId).toInstant().toEpochMilli();
LocalDateTime actualTime =
Instant.ofEpochMilli(time)
.atZone(zoneId)
.toLocalDateTime();
assertEquals(expectedTime, actualTime);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.epochconversion;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class LocalDateTimeToEpochUnitTest {
@Test
void givenDate_thenComputeEpochCorrectly() {
ZoneId zoneId = ZoneId.of("Europe/Tallinn");
long expectedEpoch = LocalDate.now().toEpochDay();
LocalDateTime givenDate = Instant.ofEpochMilli(expectedEpoch)
.atZone(zoneId)
.toLocalDateTime();
long actualEpoch = givenDate.atZone(zoneId).toInstant().toEpochMilli();
assertEquals(expectedEpoch, actualEpoch);
}
@Test
void givenTime_thenComputeEpochCorrectly() {
ZoneId zoneId = ZoneId.of("Europe/Amsterdam");
long expectedEpoch = Instant.now().toEpochMilli();
LocalDateTime givenTime = Instant.ofEpochMilli(expectedEpoch)
.atZone(zoneId)
.toLocalDateTime();
long actualEpoch = givenTime.atZone(zoneId).toInstant().toEpochMilli();
assertEquals(expectedEpoch, actualEpoch);
}
}