|
| 1 | +package com.baeldung.instantandlong; |
| 2 | + |
| 3 | + |
| 4 | +import org.junit.Test; |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | + |
| 7 | +import java.time.Instant; |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.time.ZoneId; |
| 10 | +import java.time.ZonedDateTime; |
| 11 | +import java.time.format.DateTimeFormatter; |
| 12 | +import java.time.temporal.ChronoUnit; |
| 13 | +import java.util.Locale; |
| 14 | + |
| 15 | +public class InstantAndLongUnitTest { |
| 16 | + |
| 17 | + private long someDayLong = 1_753_610_399_076L; |
| 18 | + private long oneDayLong = 86_400_000L; |
| 19 | + private String stringDate = "2025-01-30T17:33:21"; |
| 20 | + //2025.01.30 17:33:21 in milliseconds, Java epoch |
| 21 | + private long dayInMillis = ((((2025 - 1970) * 365 + 29 + 14) * 24 // days, including an additional day for each of the 14 leap years |
| 22 | + + 17) * 3600 //to seconds |
| 23 | + + 33*60 + 21) //add minutes and seconds |
| 24 | + * 1000L; //to milliseconds |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenPlusMillis_thenNextDay() { |
| 28 | + Instant someDay = Instant.ofEpochMilli(someDayLong); |
| 29 | + Instant nextDay = someDay.plusMillis(oneDayLong); |
| 30 | + |
| 31 | + assertEquals(nextDay.toEpochMilli(), someDayLong + oneDayLong); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenPlus_thenNextDay() { |
| 36 | + Instant someDay = Instant.ofEpochMilli(someDayLong); |
| 37 | + Instant nextDay = someDay.plus(oneDayLong, ChronoUnit.MILLIS); |
| 38 | + |
| 39 | + assertEquals(nextDay.toEpochMilli(), someDayLong + oneDayLong); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void whenMinusMillis_thenPreviousDay() { |
| 44 | + Instant someDay = Instant.ofEpochMilli(someDayLong); |
| 45 | + Instant previousDay = someDay.minusMillis(oneDayLong); |
| 46 | + |
| 47 | + assertEquals(previousDay.toEpochMilli(), someDayLong - oneDayLong); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void whenMinus_thenPreviousDay() { |
| 52 | + Instant someDay = Instant.ofEpochMilli(someDayLong); |
| 53 | + Instant previousDay = someDay.minus(oneDayLong, ChronoUnit.MILLIS); |
| 54 | + |
| 55 | + assertEquals(previousDay.toEpochMilli(), someDayLong - oneDayLong); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenToEpochMilli_thenDaysInMillis() { |
| 60 | + LocalDateTime dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
| 61 | + ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.of("UTC")); |
| 62 | + Instant instant = zonedDateTime.toInstant(); |
| 63 | + |
| 64 | + assertEquals(instant.toEpochMilli(), dayInMillis); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void whenOfEpochMilli_thenDateTimeAsInstant() { |
| 69 | + LocalDateTime dateTime = LocalDateTime.parse(stringDate, DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
| 70 | + ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.of("UTC")); |
| 71 | + Instant instant = zonedDateTime.toInstant(); |
| 72 | + |
| 73 | + assertEquals(Instant.ofEpochMilli(dayInMillis), instant); |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
0 commit comments