对 Java 核心的内容进行合并和调试
This commit is contained in:
@@ -8,10 +8,10 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<groupId>com.ossez.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Java Date/time conversion Cookbooks and Examples
|
||||
|
||||
This module contains articles about converting between Java date and time objects.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Converting Between LocalDate and XMLGregorianCalendar](https://www.baeldung.com/java-localdate-to-xmlgregoriancalendar)
|
||||
- [Convert Time to Milliseconds in Java](https://www.baeldung.com/java-time-milliseconds)
|
||||
- [Convert Date to LocalDate or LocalDateTime and Back](http://www.baeldung.com/java-date-to-localdate-and-localdatetime)
|
||||
- [Convert Between java.time.Instant and java.sql.Timestamp](https://www.baeldung.com/java-time-instant-to-java-sql-timestamp)
|
||||
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-datetime-conversion</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-datetime-conversion</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-datetime-conversion</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows a way to convert java.util.Date into java.time.LocalDate.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateConverter {
|
||||
|
||||
public static LocalDate convertToLocalDateViaInstant(Date dateToConvert) {
|
||||
return dateToConvert.toInstant()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) {
|
||||
return new java.sql.Date(dateToConvert.getTime()).toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDateViaMilisecond(Date dateToConvert) {
|
||||
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDate();
|
||||
}
|
||||
|
||||
public static LocalDate convertToLocalDate(Date dateToConvert) {
|
||||
return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows a way to convert java.util.Date into java.time.LocalDateTime.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateTimeConverter {
|
||||
|
||||
public static LocalDateTime convertToLocalDateTimeViaInstant(Date dateToConvert) {
|
||||
return dateToConvert.toInstant()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertToLocalDateTimeViaSqlTimestamp(Date dateToConvert) {
|
||||
return new java.sql.Timestamp(dateToConvert.getTime()).toLocalDateTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertToLocalDateTimeViaMilisecond(Date dateToConvert) {
|
||||
return Instant.ofEpochMilli(dateToConvert.getTime())
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
}
|
||||
|
||||
public static LocalDateTime convertToLocalDateTime(Date dateToConvert) {
|
||||
return LocalDateTime.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows different ways of converting java.time.LocalDateTime into java.util.Date.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateTimeToDateConverter {
|
||||
|
||||
public static Date convertToDateViaSqlTimestamp(LocalDateTime dateToConvert) {
|
||||
return java.sql.Timestamp.valueOf(dateToConvert);
|
||||
}
|
||||
|
||||
public static Date convertToDateViaInstant(LocalDateTime dateToConvert) {
|
||||
return java.util.Date.from(dateToConvert.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
}
|
||||
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Class which shows different ways of converting java.time.LocalDate into java.util.Date.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateToDateConverter {
|
||||
|
||||
public static Date convertToDateViaSqlDate(LocalDate dateToConvert) {
|
||||
return java.sql.Date.valueOf(dateToConvert);
|
||||
}
|
||||
|
||||
public static Date convertToDateViaInstant(LocalDate dateToConvert) {
|
||||
return java.util.Date.from(dateToConvert.atStartOfDay()
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toInstant());
|
||||
}
|
||||
|
||||
}
|
||||
+59
@@ -0,0 +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_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_WhenGToEpochMillis_ThenMillis() {
|
||||
java.time.Instant instant = java.time.Instant.ofEpochMilli(millis);
|
||||
Assert.assertEquals(millis, instant.toEpochMilli());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDate_WhenGetTime_ThenMillis() {
|
||||
Date date = new Date(millis);
|
||||
Assert.assertEquals(millis, date.getTime());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCalendar_WhenGetTimeInMillis_ThenMillis() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(new Date(millis));
|
||||
Assert.assertEquals(millis, calendar.getTimeInMillis());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJodaInstant_WhenGetMillis_ThenMillis() {
|
||||
Instant jodaInstant = Instant.ofEpochMilli(millis);
|
||||
Assert.assertEquals(millis, jodaInstant.getMillis());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJODADateTime_WhenGetMillis_ThenMillis() {
|
||||
org.joda.time.DateTime jodaDateTime = new org.joda.time.DateTime(millis);
|
||||
Assert.assertEquals(millis, jodaDateTime.getMillis());
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.datetime;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
//import org.joda.time.format.ISODateTimeFormat;
|
||||
//import org.joda.time.format.DateTimeFormatter;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ConvertInstantToTimestampUnitTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ConvertInstantToTimestampUnitTest.class);
|
||||
|
||||
@Test
|
||||
public void givenInstant_whenConvertedToTimestamp_thenGetTimestampWithSamePointOnTimeline() {
|
||||
Instant instant = Instant.now();
|
||||
Timestamp timestamp = Timestamp.from(instant); // same point on the time-line as Instant
|
||||
assertThat(instant.toEpochMilli()).isEqualTo(timestamp.getTime());
|
||||
|
||||
instant = timestamp.toInstant();
|
||||
assertThat(instant.toEpochMilli()).isEqualTo(timestamp.getTime());
|
||||
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
formatter = formatter.withZone(TimeZone.getTimeZone("UTC").toZoneId());
|
||||
|
||||
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||
|
||||
assertThat(formatter.format(instant)).isEqualTo(df.format(timestamp));
|
||||
}
|
||||
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.DateToLocalDateConverter;
|
||||
|
||||
/**
|
||||
* JUnits for {@link DateToLocalDateConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaInstant() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaInstant(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaMiliseconds() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaMilisecond(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaSqlDate() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDate = DateToLocalDateConverter.convertToLocalDateViaSqlDate(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDate.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDate.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDate.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertToLocalDate() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDate localDateTime = DateToLocalDateConverter.convertToLocalDate(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.DateToLocalDateTimeConverter;
|
||||
|
||||
/**
|
||||
* JUnits for {@link DateToLocalDateTimeConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class DateToLocalDateTimeConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaInstant(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaMiliseconds() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaMilisecond(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaSqlTimestamp() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTimeViaSqlTimestamp(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertToLocalDateTime() {
|
||||
// given
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2010, 10, 10, 8, 20);
|
||||
Date dateToConvert = calendar.getTime();
|
||||
|
||||
// when
|
||||
LocalDateTime localDateTime = DateToLocalDateTimeConverter.convertToLocalDateTime(dateToConvert);
|
||||
|
||||
// then
|
||||
assertEquals(2010, localDateTime.get(ChronoField.YEAR));
|
||||
assertEquals(11, localDateTime.get(ChronoField.MONTH_OF_YEAR));
|
||||
assertEquals(10, localDateTime.get(ChronoField.DAY_OF_MONTH));
|
||||
assertEquals(8, localDateTime.get(ChronoField.HOUR_OF_DAY));
|
||||
assertEquals(20, localDateTime.get(ChronoField.MINUTE_OF_HOUR));
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.LocalDateTimeToDateConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* JUnits for {@link LocalDateTimeToDateConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateTimeToDateConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
|
||||
// given
|
||||
LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20);
|
||||
|
||||
// when
|
||||
Date date = LocalDateTimeToDateConverter.convertToDateViaInstant(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(8, calendar.get(Calendar.HOUR));
|
||||
assertEquals(20, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaSqlTimestamp() {
|
||||
// given
|
||||
LocalDateTime dateToConvert = LocalDateTime.of(2010, 11, 10, 8, 20);
|
||||
|
||||
// when
|
||||
Date date = LocalDateTimeToDateConverter.convertToDateViaSqlTimestamp(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
assertEquals(8, calendar.get(Calendar.HOUR));
|
||||
assertEquals(20, calendar.get(Calendar.MINUTE));
|
||||
assertEquals(0, calendar.get(Calendar.SECOND));
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.datetolocaldate;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.datetolocaldate.LocalDateToDateConverter;
|
||||
|
||||
/**
|
||||
*
|
||||
* JUnits for {@link LocalDateToDateConverter} class.
|
||||
*
|
||||
* @author abialas
|
||||
*
|
||||
*/
|
||||
public class LocalDateToDateConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaInstant() {
|
||||
// given
|
||||
LocalDate dateToConvert = LocalDate.of(2010, 11, 10);
|
||||
|
||||
// when
|
||||
Date date = LocalDateToDateConverter.convertToDateViaInstant(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturn10thNovember2010WhenConvertViaSqlDate() {
|
||||
// given
|
||||
LocalDate dateToConvert = LocalDate.of(2010, 11, 10);
|
||||
|
||||
// when
|
||||
Date date = LocalDateToDateConverter.convertToDateViaSqlDate(dateToConvert);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
|
||||
// then
|
||||
assertEquals(2010, calendar.get(Calendar.YEAR));
|
||||
assertEquals(10, calendar.get(Calendar.MONTH));
|
||||
assertEquals(10, calendar.get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.xmlgregoriancalendar;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeConstants;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class XmlGregorianCalendarConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void fromLocalDateToXMLGregorianCalendar() throws DatatypeConfigurationException {
|
||||
LocalDate localDate = LocalDate.of(2017, 4, 25);
|
||||
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(localDate.toString());
|
||||
|
||||
assertThat(xmlGregorianCalendar.getYear()).isEqualTo(localDate.getYear());
|
||||
assertThat(xmlGregorianCalendar.getMonth()).isEqualTo(localDate.getMonthValue());
|
||||
assertThat(xmlGregorianCalendar.getDay()).isEqualTo(localDate.getDayOfMonth());
|
||||
assertThat(xmlGregorianCalendar.getTimezone()).isEqualTo(DatatypeConstants.FIELD_UNDEFINED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromXMLGregorianCalendarToLocalDate() throws DatatypeConfigurationException {
|
||||
XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar("2017-04-25");
|
||||
LocalDate localDate = LocalDate.of(xmlGregorianCalendar.getYear(), xmlGregorianCalendar.getMonth(), xmlGregorianCalendar.getDay());
|
||||
|
||||
assertThat(localDate.getYear()).isEqualTo(xmlGregorianCalendar.getYear());
|
||||
assertThat(localDate.getMonthValue()).isEqualTo(xmlGregorianCalendar.getMonth());
|
||||
assertThat(localDate.getDayOfMonth()).isEqualTo(xmlGregorianCalendar.getDay());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-io</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>${project.parent.version}</version>
|
||||
<name>core-java-io</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
<groupId>com.ossez.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -76,7 +76,7 @@
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
@@ -105,7 +105,8 @@
|
||||
<dependency>
|
||||
<groupId>org.javamoney</groupId>
|
||||
<artifactId>moneta</artifactId>
|
||||
<version>${moneta.version}</version>
|
||||
<version>1.4.2</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.owasp.esapi</groupId>
|
||||
@@ -198,7 +199,7 @@
|
||||
<argument>-Xmx300m</argument>
|
||||
<argument>-XX:+UseParallelGC</argument>
|
||||
<argument>-classpath</argument>
|
||||
<classpath />
|
||||
<classpath/>
|
||||
<argument>com.ossez.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
@@ -212,7 +213,7 @@
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
||||
<plugin>
|
||||
<!-- Build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
@@ -252,7 +253,7 @@
|
||||
<executable>java</executable>
|
||||
<arguments>
|
||||
<argument>-classpath</argument>
|
||||
<classpath />
|
||||
<classpath/>
|
||||
<argument>org.openjdk.jmh.Main</argument>
|
||||
<argument>.*</argument>
|
||||
</arguments>
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.ossez.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<name>core-java-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.ossez</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>pre-jpms</module>
|
||||
<module>core-java-optional</module>
|
||||
<module>core-java-lang-operators</module>
|
||||
<module>core-java-networking-2</module>
|
||||
<module>core-java-security-manager</module>
|
||||
<module>core-java-annotations</module>
|
||||
<module>core-java-datetime-conversion</module>
|
||||
<module>core-java-io</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user