BAEL-5666 - Create Date from Unix timestamp in Java (#12650)

* moving SpringBootPersistenceApplication class to its own package

* from com.baeldung to com.baeldung.logging to prevent it from loading
contexts from other applications.

* moving SpringBootPersistenceApplication class to its own package

* from com.baeldung to com.baeldung.logging to prevent it from loading
contexts from other applications.

* Spring Data MongoDB - Configure Connection

Ready for revision.

* fixed tests to reflect article changes

* BAEL-5657

* reverting BAEL-5657

* reverting BAEL-5657

* ready for review

* removing bael-5366

* removing bael-5366

* removing extra blank space

* bael-5666

editor review

* editor review

* long literals
* junit 5
* private constructor for utils

* junit5 assertThat
This commit is contained in:
Ulisses Lima
2022-09-16 17:31:27 -03:00
committed by GitHub
parent b868bf6440
commit 3577acbabb
13 changed files with 208 additions and 325 deletions

View File

@@ -0,0 +1,60 @@
package com.baeldung.unixtime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class UnixTimeUtils {
private UnixTimeUtils() {
}
public static Date dateFrom(long timestamp) {
return new Date(timestamp);
}
public static Calendar calendarFrom(long timestamp) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timestamp);
return calendar;
}
public static Instant fromNanos(long timestamp) {
long seconds = timestamp / 1_000_000_000;
long nanos = timestamp % 1_000_000_000;
return Instant.ofEpochSecond(seconds, nanos);
}
public static Instant fromTimestamp(long timestamp) {
return Instant.ofEpochMilli(millis(timestamp));
}
public static String format(Instant instant) {
LocalDateTime time = localTimeUtc(instant);
return time.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
public static LocalDateTime localTimeUtc(Instant instant) {
return LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
}
private static long millis(long timestamp) {
if (timestamp >= 1E16 || timestamp <= -1E16) {
return timestamp / 1_000_000;
}
if (timestamp >= 1E14 || timestamp <= -1E14) {
return timestamp / 1_000;
}
if (timestamp >= 1E11 || timestamp <= -3E10) {
return timestamp;
}
return timestamp * 1_000;
}
}

View File

@@ -0,0 +1,104 @@
package com.baeldung.unixtime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import org.junit.jupiter.api.Test;
class UnixTimeUtilsUnitTest {
private static final String AUG_16_2022_15h25m32_Z_FORMATTED = "2022-08-16T15:25:32";
private static final long AUG_16_2022_15h25m32_Z_NANOS = 1660663532747420283L;
private static final long AUGUST = 8;
private void assertInstantFieldsMatch(LocalDateTime time) {
assertEquals(AUGUST, time.get(ChronoField.MONTH_OF_YEAR));
assertEquals(16, time.get(ChronoField.DAY_OF_MONTH));
assertEquals(2022, time.get(ChronoField.YEAR));
assertEquals(15, time.get(ChronoField.HOUR_OF_DAY));
assertEquals(25, time.get(ChronoField.MINUTE_OF_HOUR));
assertEquals(32, time.get(ChronoField.SECOND_OF_MINUTE));
}
@Test
void givenMillis_whenDateFrom_thenLocalTimeMatches() {
long millis = AUG_16_2022_15h25m32_Z_NANOS / 1000 / 1000;
Date date = UnixTimeUtils.dateFrom(millis);
LocalDateTime time = UnixTimeUtils.localTimeUtc(date.toInstant());
assertInstantFieldsMatch(time);
}
@Test
void givenMillis_whenCalendarFrom_thenLocalTimeMatches() {
long millis = AUG_16_2022_15h25m32_Z_NANOS / 1000 / 1000;
Calendar calendar = UnixTimeUtils.calendarFrom(millis);
LocalDateTime time = UnixTimeUtils.localTimeUtc(calendar.toInstant());
assertInstantFieldsMatch(time);
}
@Test
void whenInstantFromNanos_thenLocalTimeMatches() {
Instant instant = UnixTimeUtils.fromNanos(AUG_16_2022_15h25m32_Z_NANOS);
LocalDateTime time = UnixTimeUtils.localTimeUtc(instant);
assertThat(time.toString()).startsWith(AUG_16_2022_15h25m32_Z_FORMATTED);
}
@Test
void givenWrongPrecision_whenInstantFromNanos_thenUnexpectedTime() {
long microseconds = AUG_16_2022_15h25m32_Z_NANOS / 1000;
Instant instant = UnixTimeUtils.fromNanos(microseconds);
LocalDateTime time = UnixTimeUtils.localTimeUtc(instant);
assertThat(time.toString()).doesNotStartWith(AUG_16_2022_15h25m32_Z_FORMATTED);
assertEquals("1970-01-20T05:17:43.532747420", time.toString());
}
@Test
void givenNanos_whenInstantFromTimestamp_thenLocalTimeMatches() {
Instant instant = UnixTimeUtils.fromTimestamp(AUG_16_2022_15h25m32_Z_NANOS);
LocalDateTime time = UnixTimeUtils.localTimeUtc(instant);
assertInstantFieldsMatch(time);
}
@Test
void givenMicroseconds_whenInstantFromTimestamp_thenLocalTimeMatches() {
long microseconds = AUG_16_2022_15h25m32_Z_NANOS / 1000;
Instant instant = UnixTimeUtils.fromTimestamp(microseconds);
LocalDateTime time = UnixTimeUtils.localTimeUtc(instant);
assertInstantFieldsMatch(time);
}
@Test
void givenMillis_whenInstantFromTimestamp_thenLocalTimeMatches() {
long millis = AUG_16_2022_15h25m32_Z_NANOS / 1000 / 1000;
Instant instant = UnixTimeUtils.fromTimestamp(millis);
LocalDateTime time = UnixTimeUtils.localTimeUtc(instant);
assertInstantFieldsMatch(time);
}
@Test
void givenSeconds_whenInstantFromTimestamp_thenLocalTimeMatches() {
long seconds = AUG_16_2022_15h25m32_Z_NANOS / 1000 / 1000 / 1000;
Instant instant = UnixTimeUtils.fromTimestamp(seconds);
LocalDateTime time = UnixTimeUtils.localTimeUtc(instant);
assertInstantFieldsMatch(time);
}
}