JUnit to output millis

This commit is contained in:
mherbaghinyan 2019-04-17 12:05:08 +04:00
parent eff19c7163
commit f3f4c32f01
4 changed files with 65 additions and 50 deletions

View File

@ -8,6 +8,13 @@
<artifactId>java-dates-2</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>joda-time</groupId>

View File

@ -1,50 +0,0 @@
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 void main(String[] args) {
java8();
joda();
Date date = coreDate();
calendar(date);
}
private static Date coreDate() {
Date date = new Date();
System.out.println("Date - Time in milliseconds : " + date.getTime());
return date;
}
private static void calendar(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println("Calender - Time in milliseconds : " + calendar.getTimeInMillis());
}
private static void joda() {
org.joda.time.DateTime jodaDateTime = new org.joda.time.DateTime();
long delta = jodaDateTime.getMillis();
System.out.println("Joda - Time in milliseconds : " + delta);
Instant jodaInstant = Instant.now();
System.out.println("Joda - Instant in milliseconds : " + jodaInstant.getMillis());
}
private static void java8() {
long instantMillis = java.time.Instant.now().toEpochMilli();
LocalDateTime localDateTime = LocalDateTime.now();
ZoneId id = ZoneId.systemDefault();
ZonedDateTime zdt = ZonedDateTime.of(localDateTime, id);
System.out.println("Java 8 - Time in milliseconds : " + zdt.toInstant().toEpochMilli());
}
}

View File

@ -0,0 +1,45 @@
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

@ -0,0 +1,13 @@
package com.baeldung.convert;
import org.junit.Test;
public class ConvertDateTimeUnitTest {
@Test
public void givenLocalDateTime_WhenGetMillis() {
long millis = ConvertDateTime.java8LocalDateTime();
System.out.println("LocalDateTime in milliseconds : " + millis);
}
}