[BAEL-6593] - Initial push after adding epoch time to localdate and localdatetime conversions (#14556)

Co-authored-by: bobby00 <edwarobert6@gmail.com>
This commit is contained in:
Robaed 2023-08-10 10:05:12 +03:00 committed by GitHub
parent e9d3138d65
commit b04d773ef2
4 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.epochtolocaldate;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/**
* Class which shows a way to convert Epoch time in milliseconds to java.time.LocalDate.
*
* @author quincy
*
*/
public class EpochTimeToLocalDateConverter {
public static String main(String[] args) {
long epochTimeinMillisToConvert = 1624962431000L;
Instant instant = Instant.ofEpochMilli(epochTimeinMillisToConvert);
ZoneId zoneId = ZoneId.systemDefault(); // Use the system default time zone
LocalDate localDate = instant.atZone(zoneId).toLocalDate();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = localDate.format(formatter);
return formattedDate;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.epochtolocaldate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
/**
* Class which shows a way to convert Epoch time in milliseconds to java.time.LocalDateTime.
*
* @author quincy
*
*/
public class EpochTimeToLocalDateTimeConverter {
public static String main(String[] args) {
long epochTimeinMillisToConvert = 1624962431000L;
Instant instant = Instant.ofEpochMilli(epochTimeinMillisToConvert);
ZoneId zoneId = ZoneId.systemDefault(); // Use the system default time zone
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = localDateTime.format(formatter);
return formattedDate;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.epochtolocaldate;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EpochTimeToLocalDateConverterUnitTest {
@Test
public void testConvertEpochTimeToLocalDate() {
long epochTimeMillis = 1624962431000L; // Example epoch time in millisecond
LocalDate expectedDate = LocalDate.of(2021, 6, 29);
Instant instant = Instant.ofEpochMilli(epochTimeMillis);
ZoneId zoneId = ZoneId.systemDefault();
LocalDate actualDate = instant.atZone(zoneId).toLocalDate();
assertEquals(expectedDate, actualDate);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.epochtolocaldate;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EpochTimeToLocalDateTimeConverterUnitTest {
@Test
public void testConvertEpochTimeToLocalDateTime() {
long epochTimeMillis = 1624962431000L; // Example epoch time in milliseconds
LocalDateTime expectedDateTime = LocalDateTime.of(2021, 6, 29, 12, 13, 51);
Instant instant = Instant.ofEpochMilli(epochTimeMillis);
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime actualDateTime = instant.atZone(zoneId).toLocalDateTime();
assertEquals(expectedDateTime, actualDateTime);
}
}