move the code to unit tests

This commit is contained in:
mherbaghinyan 2019-04-25 14:06:00 +04:00
parent 637f2e945a
commit f1ef8d9335
2 changed files with 35 additions and 63 deletions

View File

@ -1,45 +0,0 @@
package com.baeldung.convert;
import org.joda.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
public class ConvertDateTime {
public static long coreDate() {
Date date = new Date();
return date.getTime();
}
public static long calendar() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
return calendar.getTimeInMillis();
}
public static long jodaInstant() {
Instant jodaInstant = Instant.now();
return jodaInstant.getMillis();
}
public static long jodaDateTime() {
org.joda.time.DateTime jodaDateTime = new org.joda.time.DateTime();
return jodaDateTime.getMillis();
}
public static long java8Instant() {
return java.time.Instant.now().toEpochMilli();
}
public static long java8LocalDateTime() {
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId id = ZoneId.systemDefault();
ZonedDateTime zdt = ZonedDateTime.of(localDateTime, id);
return zdt.toInstant().toEpochMilli();
}
}

View File

@ -1,42 +1,59 @@
package com.baeldung.convert;
import org.joda.time.Instant;
import org.junit.Assert;
import org.junit.Test;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
public class ConvertDateTimeUnitTest {
public final long millis = 1556175797428L;
@Test
public void givenLocalDateTime_WhenGetMillis() {
long millis = ConvertDateTime.java8LocalDateTime();
System.out.println("LocalDateTime in milliseconds : " + millis);
public void givenLocalDateTime_WhenToEpochMillis_ThenMillis() {
ZoneId id = ZoneId.systemDefault();
LocalDateTime localDateTime =
LocalDateTime.ofInstant(java.time.Instant.ofEpochMilli(millis), id);
ZonedDateTime zdt = ZonedDateTime.of(localDateTime, id);
Assert.assertEquals(millis, zdt.toInstant().toEpochMilli());
}
@Test
public void givenJava8Instant_WhenGetMillis() {
long millis = ConvertDateTime.java8Instant();
System.out.println("Java 8 Instant in milliseconds : " + millis);
public void givenJava8Instant_WhenGToEpochMillis_ThenMillis() {
java.time.Instant instant = java.time.Instant.ofEpochMilli(millis);
Assert.assertEquals(millis, instant.toEpochMilli());
}
@Test
public void givenDate_WhenGetMillis() {
long millis = ConvertDateTime.coreDate();
System.out.println("Core Date in milliseconds : " + millis);
public void givenDate_WhenGetTime_ThenMillis() {
Date date = new Date(millis);
Assert.assertEquals(millis, date.getTime());
}
@Test
public void givenCalendar_WhenGetMillis() {
long millis = ConvertDateTime.calendar();
System.out.println("Calendar in milliseconds : " + millis);
public void givenCalendar_WhenGetTimeInMillis_ThenMillis() {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(millis));
Assert.assertEquals(millis, calendar.getTimeInMillis());
}
@Test
public void givenJodaInstant_WhenGetMillis() {
long millis = ConvertDateTime.jodaInstant();
System.out.println("JODA Instant in milliseconds : " + millis);
public void givenJodaInstant_WhenGetMillis_ThenMillis() {
Instant jodaInstant = Instant.ofEpochMilli(millis);
Assert.assertEquals(millis, jodaInstant.getMillis());
}
@Test
public void givenJODADateTime_WhenGetMillis() {
long millis = ConvertDateTime.jodaDateTime();
System.out.println("JODA DateTime in milliseconds : " + millis);
public void givenJODADateTime_WhenGetMillis_ThenMillis() {
org.joda.time.DateTime jodaDateTime = new org.joda.time.DateTime(millis);
Assert.assertEquals(millis, jodaDateTime.getMillis());
}
}