Converted class to JUnit Test

This commit is contained in:
Yatendra Goel 2018-11-03 15:59:05 +05:30
parent 7064a7e309
commit 5e1fc54ec2

View File

@ -1,6 +1,6 @@
package com.baeldung.datetime;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import java.sql.Timestamp;
import java.text.DateFormat;
@ -9,24 +9,22 @@ import java.time.Instant;
import java.time.ZoneId;
import java.util.TimeZone;
public class ConvertInstantToTimestamp {
import org.junit.Test;
public static void main(String[] args) {
run();
}
public class ConvertInstantToTimestampTest {
public static void run() {
@Test
public void givenInstant_whenConvertedToTimestamp_thenGetTimestampWithSamePointOnTimeline() {
Instant instant = Instant.now();
Timestamp timestamp = Timestamp.from(instant); // same point on the time-line as Instant
assertEquals(instant.toEpochMilli(), timestamp.getTime());
assertThat(instant.toEpochMilli()).isEqualTo(timestamp.getTime());
instant = timestamp.toInstant();
assertEquals(instant.toEpochMilli(), timestamp.getTime());
assertThat(instant.toEpochMilli()).isEqualTo(timestamp.getTime());
DateFormat df = DateFormat.getDateTimeInstance();
df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SS'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
assertEquals(instant.toString(), df.format(timestamp).toString());
assertThat(instant.toString()).isEqualTo(df.format(timestamp).toString());
}
}