Merge pull request #15285 from Michaelin007/unixtime

Retrieving Unix Time in Java
This commit is contained in:
Maiklins 2023-11-30 16:05:58 +01:00 committed by GitHub
commit df2a1a7501
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.unixtime;
import static org.junit.Assert.assertEquals;
import java.time.Instant;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.util.Date;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;
public class UnixTimeUnitTest {
@Test
public void givenTimeUsingDateApi_whenConvertedToUnixTime_thenMatch() {
Date date = new Date(2023 - 1900, 1, 15, 0, 0, 0);
long expected = 1676419200;
long actual = date.getTime() / 1000L;
assertEquals(expected, actual);
}
@Test
public void givenTimeUsingJodaTime_whenConvertedToUnixTime_thenMatch() {
DateTime dateTime = new DateTime(2023, 2, 15, 00, 00, 00, 0);
long expected = 1676419200;
long actual = dateTime.getMillis() / 1000L;
assertEquals(expected, actual);
}
@Test
public void givenTimeUsingLocalDate_whenConvertedToUnixTime_thenMatch() {
LocalDate date = LocalDate.of(2023, Month.FEBRUARY, 15);
Instant instant = date.atStartOfDay().atZone(ZoneId.of("UTC")).toInstant();
long expected = 1676419200;
long actual = instant.getEpochSecond();
assertEquals(expected, actual);
}
}