Retrieving Unix Time in Java
This commit is contained in:
parent
c519d2be47
commit
fa272d3395
@ -1,50 +1,42 @@
|
|||||||
package com.baeldung.unixtime;
|
package com.baeldung.unixtime;
|
||||||
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import java.time.Month;
|
||||||
|
import java.time.ZoneId;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
import org.joda.time.DateTimeZone;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class UnixTimeUnitTest {
|
public class UnixTimeUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCurrentTimeUsingDateApi_whenConvertedToUnixTime_thenConsistent() {
|
public void givenTimeUsingDateApi_whenConvertedToUnixTime_thenMatch() {
|
||||||
long marginOfError = 1L;
|
Date date = new Date(2023 - 1900, 1, 15, 0, 0, 0);
|
||||||
Date date = new Date();
|
long expected = 1676419200;
|
||||||
long longTime = date.getTime() / 1000L;
|
long actual = date.getTime() / 1000L;
|
||||||
|
assertEquals(expected, actual);
|
||||||
for (int i = 0; i < 10; i++) {
|
|
||||||
long unixTime = System.currentTimeMillis() / 1000L;
|
|
||||||
assertEquals(longTime, unixTime, marginOfError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCurrentTimeUsingInstantNow_whenConvertedToUnixTime_thenConsistent() {
|
public void givenTimeUsingJodaTime_whenConvertedToUnixTime_thenMatch() {
|
||||||
long marginOfError = 1L;
|
DateTime dateTime = new DateTime(2023, 2, 15, 00, 00, 00, 0);
|
||||||
long longTime = Instant.now().getEpochSecond();
|
long expected = 1676419200;
|
||||||
|
long actual = dateTime.getMillis() / 1000L;
|
||||||
for (int i = 0; i < 10; i++) {
|
assertEquals(expected, actual);
|
||||||
long unixTime = System.currentTimeMillis() / 1000L;
|
|
||||||
assertEquals(longTime, unixTime, marginOfError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCurrentTimeUsingJodaTime_whenConvertedToUnixTime_thenConsistent() {
|
public void givenTimeUsingLocalDate_whenConvertedToUnixTime_thenMatch() {
|
||||||
long marginOfError = 1L;
|
LocalDate date = LocalDate.of(2023, Month.FEBRUARY, 15);
|
||||||
DateTime dateTime = DateTime.now(DateTimeZone.UTC);
|
Instant instant = date.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant();
|
||||||
long longTime = dateTime.getMillis() / 1000L;
|
long expected = 1676419200;
|
||||||
|
long actual = instant.getEpochSecond();
|
||||||
for (int i = 0; i < 10; i++) {
|
assertEquals(expected, actual);
|
||||||
long unixTime = System.currentTimeMillis() / 1000L;
|
|
||||||
assertEquals(longTime, unixTime, marginOfError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user