BAEL-6357: Convert Long to Date in Java (#15691)
Co-authored-by: Luis Javier Peris Morillo <javierperis@gmail.com>
This commit is contained in:
parent
dcb4eecaf5
commit
899104d579
@ -3,4 +3,3 @@ This module contains articles about date operations in Java.
|
|||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>core-java-date-operations-4</artifactId>
|
<artifactId>core-java-date-operations-4</artifactId>
|
||||||
<name>core-java-date-operations-4</name>
|
<name>core-java-date-operations-4</name>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -39,7 +40,7 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<joda-time.version>2.12.5</joda-time.version>
|
<joda-time.version>2.12.6</joda-time.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -0,0 +1,74 @@
|
|||||||
|
package com.baeldung.longtodate;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import org.joda.time.DateTimeZone;
|
||||||
|
import org.joda.time.Instant;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class LongToDateUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLongValue_whenUsingInstantClass_thenConvert() {
|
||||||
|
Instant expectedDate = Instant.parse("2020-09-08T12:16:40Z");
|
||||||
|
long seconds = 1599567400L;
|
||||||
|
|
||||||
|
Instant date = Instant.ofEpochSecond(seconds);
|
||||||
|
|
||||||
|
assertEquals(expectedDate, date);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLongValue_whenUsingLocalDateClass_thenConvert() {
|
||||||
|
LocalDate expectedDate = LocalDate.of(2023, 10, 17);
|
||||||
|
long epochDay = 19647L;
|
||||||
|
|
||||||
|
LocalDate date = LocalDate.ofEpochDay(epochDay);
|
||||||
|
|
||||||
|
assertEquals(expectedDate, date);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLongValue_whenUsingDateClass_thenConvert() throws ParseException {
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
Date expectedDate = dateFormat.parse("2023-07-15 22:00:00");
|
||||||
|
long milliseconds = 1689458400000L;
|
||||||
|
|
||||||
|
Date date = new Date(milliseconds);
|
||||||
|
|
||||||
|
assertEquals(expectedDate, date);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLongValue_whenUsingCalendarClass_thenConvert() throws ParseException {
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
Date expectedDate = dateFormat.parse("2023-07-15 22:00:00");
|
||||||
|
long milliseconds = 1689458400000L;
|
||||||
|
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
calendar.setTimeInMillis(milliseconds);
|
||||||
|
|
||||||
|
assertEquals(expectedDate, calendar.getTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenLongValue_whenUsingJodaTimeLocalDateClass_thenConvert() {
|
||||||
|
org.joda.time.LocalDate expectedDate = new org.joda.time.LocalDate(2023, 7, 15);
|
||||||
|
long milliseconds = 1689458400000L;
|
||||||
|
|
||||||
|
org.joda.time.LocalDate date = new org.joda.time.LocalDate(milliseconds, DateTimeZone.UTC);
|
||||||
|
|
||||||
|
assertEquals(expectedDate, date);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user