michaelin007 2023-11-24 09:35:01 +00:00
parent c10a404f57
commit c519d2be47
1 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package com.baeldung.unixtime;
import static org.junit.Assert.assertEquals;
import java.time.Instant;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.jupiter.api.Test;
public class UnixTimeUnitTest {
@Test
public void givenCurrentTimeUsingDateApi_whenConvertedToUnixTime_thenConsistent() {
long marginOfError = 1L;
Date date = new Date();
long longTime = date.getTime() / 1000L;
for (int i = 0; i < 10; i++) {
long unixTime = System.currentTimeMillis() / 1000L;
assertEquals(longTime, unixTime, marginOfError);
}
}
@Test
public void givenCurrentTimeUsingInstantNow_whenConvertedToUnixTime_thenConsistent() {
long marginOfError = 1L;
long longTime = Instant.now().getEpochSecond();
for (int i = 0; i < 10; i++) {
long unixTime = System.currentTimeMillis() / 1000L;
assertEquals(longTime, unixTime, marginOfError);
}
}
@Test
public void givenCurrentTimeUsingJodaTime_whenConvertedToUnixTime_thenConsistent() {
long marginOfError = 1L;
DateTime dateTime = DateTime.now(DateTimeZone.UTC);
long longTime = dateTime.getMillis() / 1000L;
for (int i = 0; i < 10; i++) {
long unixTime = System.currentTimeMillis() / 1000L;
assertEquals(longTime, unixTime, marginOfError);
}
}
}