Skip to content

Instantly share code, notes, and snippets.

@jwgmeligmeyling
Created January 20, 2016 21:43
Show Gist options
  • Save jwgmeligmeyling/493d80ee5c85296e4ae8 to your computer and use it in GitHub Desktop.
Save jwgmeligmeyling/493d80ee5c85296e4ae8 to your computer and use it in GitHub Desktop.
Some tests on basic assumptions on Java's LocalDateTime
import org.junit.Test;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import static org.junit.Assert.assertEquals;
/**
* @author Jan-Willem Gmelig Meyling
*/
public class DateTest {
private static ZoneId AMSTERDAM = ZoneId.of("Europe/Amsterdam");
@Test
public void verifyEpochIsConvertedToSameDate() throws Exception {
long now = System.currentTimeMillis();
Instant instant = Instant.ofEpochMilli(now);
LocalDateTime res = LocalDateTime.ofInstant(instant, AMSTERDAM);
assertEquals(now, res.atZone(AMSTERDAM).toInstant().toEpochMilli());
}
@Test
public void verifyEpochIsConvertedToSameDateInSummer() throws Exception {
LocalDateTime dateInSummer = LocalDateTime.of(2015, 4, 1, 9, 0);
long now = dateInSummer.atZone(AMSTERDAM).toInstant().toEpochMilli();
Instant instant = Instant.ofEpochMilli(now);
LocalDateTime res = LocalDateTime.ofInstant(instant, AMSTERDAM);
assertEquals(now, res.atZone(AMSTERDAM).toInstant().toEpochMilli());
// Let's see whether we agree on the date variables
assertEquals(2015, res.getYear());
assertEquals(4, res.getMonthValue());
assertEquals(1, res.getDayOfMonth());
assertEquals(9, res.getHour());
assertEquals(0, res.getMinute());
// Verify that the 4th month is indeed April, and we start counting from 1.
assertEquals(Month.APRIL, res.getMonth());
}
@Test
public void verifyEpochIsConvertedToSameDateInWinter() throws Exception {
LocalDateTime dateInSummer = LocalDateTime.of(2015, 10, 26, 9, 0);
long now = dateInSummer.atZone(AMSTERDAM).toInstant().toEpochMilli();
Instant instant = Instant.ofEpochMilli(now);
LocalDateTime res = LocalDateTime.ofInstant(instant, AMSTERDAM);
assertEquals(now, res.atZone(AMSTERDAM).toInstant().toEpochMilli());
// Let's see whether we agree on the date variables
assertEquals(2015, res.getYear());
assertEquals(10, res.getMonthValue());
assertEquals(26, res.getDayOfMonth());
assertEquals(9, res.getHour());
assertEquals(0, res.getMinute());
// Verify that the 4th month is indeed October, and we start counting from 1.
assertEquals(Month.OCTOBER, res.getMonth());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment