对 Java 核心的内容进行合并和调试

This commit is contained in:
YuCheng Hu 2021-08-13 15:33:33 -04:00
parent f8d0bd94bc
commit 1654988fe3
25 changed files with 770 additions and 53 deletions

View File

@ -2,18 +2,26 @@
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile default="true" name="Default" enabled="true" />
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="codebank" />
<module name="core-java-io" />
<module name="core-java-datetime-conversion" />
<module name="java-collections-conversions" />
<module name="java-collections-conversions-2" />
<module name="core-java-annotations" />
<module name="discourse" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="codebank" target="11" />
<module name="core-java-annotations" target="11" />
<module name="core-java-datetime-conversion" target="1.9" />
<module name="core-java-io" target="11" />
<module name="core-java-modules" target="11" />
<module name="discourse" target="11" />
<module name="java-collections-conversions" target="11" />
<module name="java-collections-conversions-2" target="11" />

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-io/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-io/src/main/resources" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/java-collections-conversions-2/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/java-collections-conversions/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/java-collections-conversions/src/main/resources" charset="UTF-8" />

View File

@ -6,6 +6,7 @@
<option value="$PROJECT_DIR$/pom.xml" />
<option value="$PROJECT_DIR$/java-collections-conversions/pom.xml" />
<option value="$PROJECT_DIR$/java-collections-conversions-2/pom.xml" />
<option value="$PROJECT_DIR$/core-java-modules/pom.xml" />
</list>
</option>
</component>

View File

@ -3,6 +3,10 @@
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/toolkits/codebank/codebank.iml" filepath="$PROJECT_DIR$/toolkits/codebank/codebank.iml" />
<module fileurl="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/core-java-annotations.iml" filepath="$PROJECT_DIR$/core-java-modules/core-java-annotations/core-java-annotations.iml" />
<module fileurl="file://$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/core-java-datetime-conversion.iml" filepath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/core-java-datetime-conversion.iml" />
<module fileurl="file://$PROJECT_DIR$/core-java-modules/core-java-io/core-java-io.iml" filepath="$PROJECT_DIR$/core-java-modules/core-java-io/core-java-io.iml" />
<module fileurl="file://$PROJECT_DIR$/core-java-modules/core-java-modules.iml" filepath="$PROJECT_DIR$/core-java-modules/core-java-modules.iml" />
<module fileurl="file://$PROJECT_DIR$/toolkits/discourse/discourse.iml" filepath="$PROJECT_DIR$/toolkits/discourse/discourse.iml" />
<module fileurl="file://$PROJECT_DIR$/java-collections-conversions/java-collections-conversions.iml" filepath="$PROJECT_DIR$/java-collections-conversions/java-collections-conversions.iml" />
<module fileurl="file://$PROJECT_DIR$/java-collections-conversions-2/java-collections-conversions-2.iml" filepath="$PROJECT_DIR$/java-collections-conversions-2/java-collections-conversions-2.iml" />

View File

@ -4,33 +4,32 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="e4ccec40-1722-49be-ba56-1cba428e2e66" name="Default Changelist" comment="Change code for full project compile">
<change afterPath="$PROJECT_DIR$/.idea/runConfigurations.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/README.md" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/convertlisttomap/ListToMapConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/MapperUtil.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/User.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserDTO.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserList.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UserListDTO.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/main/java/com/baeldung/modelmapper/UsersListConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/test/java/com/baeldung/convertarraytostring/ArrayToStringUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/test/java/com/baeldung/convertlisttomap/ListToMapUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/test/java/com/baeldung/modelmapper/UsersListMappingUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/java-collections-conversions-2/src/test/java/com/baeldung/setiteration/SetIteration.java" afterDir="false" />
<list default="true" id="e4ccec40-1722-49be-ba56-1cba428e2e66" name="Default Changelist" comment="修正项目结构">
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/README.md" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/pom.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/datetolocaldate/DateToLocalDateConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/datetolocaldate/DateToLocalDateTimeConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/datetolocaldate/LocalDateTimeToDateConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/datetolocaldate/LocalDateToDateConverter.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/convert/ConvertDateTimeUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/datetime/ConvertInstantToTimestampUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/datetolocaldate/DateToLocalDateConverterUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/datetolocaldate/DateToLocalDateTimeConverterUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/datetolocaldate/LocalDateTimeToDateConverterUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/datetolocaldate/LocalDateToDateConverterUnitTest.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/xmlgregoriancalendar/XmlGregorianCalendarConverterUnitTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/compiler.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/compiler.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/encodings.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/encodings.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/java-collections-conversions/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/java-collections-conversions/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/java-collections-conversions/src/test/java/com/ossez/arrayconversion/ArrayToListConversionUnitTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/java-collections-conversions/src/test/java/com/ossez/arrayconversion/ArrayToListConversionUnitTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/parent-java/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/parent-java/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/core-java-modules/core-java-annotations/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/core-java-modules/core-java-annotations/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/core-java-modules/core-java-io/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/core-java-modules/core-java-io/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/core-java-modules/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/core-java-modules/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/java-collections-conversions-2/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/java-collections-conversions-2/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/persistence-modules/activejdbc/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/persistence-modules/activejdbc/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/toolkits/codebank/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/toolkits/codebank/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/toolkits/discourse/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/toolkits/discourse/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/com/ossez/codebank/algorithm/tests/VariableTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/com/ossez/codebank/algorithm/tests/VariableTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -54,9 +53,10 @@
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="TF_FMT" value="false" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/java-collections-conversions-2/pom.xml" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/core-java-modules/pom.xml" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
@ -73,7 +73,7 @@
<recent name="D:\WorkDir\Repository\GitHub\cwiki-us-docs\java-tutorials\parent-java" />
</key>
</component>
<component name="RunManager" selected="JUnit.ArrayToListConversionUnitTest">
<component name="RunManager" selected="JUnit.ConvertInstantToTimestampUnitTest.testISO1806">
<configuration default="true" type="ArquillianJUnit" factoryName="" nameIsGenerated="true">
<option name="arquillianRunConfiguration">
<value>
@ -116,8 +116,25 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="ConvertInstantToTimestampUnitTest.testISO1806" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="core-java-datetime-conversion" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="com.baeldung.datetime.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="com.baeldung.datetime" />
<option name="MAIN_CLASS_NAME" value="com.baeldung.datetime.ConvertInstantToTimestampUnitTest" />
<option name="METHOD_NAME" value="testISO1806" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.ConvertInstantToTimestampUnitTest.testISO1806" />
<item itemvalue="JUnit.ArrayToListConversionUnitTest" />
<item itemvalue="JUnit.ArrayToListConversionUnitTest.givenAnArray_whenConvertingToList_returnUnmodifiableListUnitTest" />
</list>
@ -133,6 +150,8 @@
<updated>1619410137650</updated>
<workItem from="1619410141685" duration="410000" />
<workItem from="1628774960541" duration="4091000" />
<workItem from="1628808291113" duration="100000" />
<workItem from="1628871072447" duration="9867000" />
</task>
<task id="LOCAL-00001" summary="Change code for full project compile">
<created>1619410303516</created>
@ -141,7 +160,14 @@
<option name="project" value="LOCAL" />
<updated>1619410303516</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="修正项目结构">
<created>1628808340193</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1628808340193</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -161,6 +187,7 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="Change code for full project compile" />
<option name="LAST_COMMIT_MESSAGE" value="Change code for full project compile" />
<MESSAGE value="修正项目结构" />
<option name="LAST_COMMIT_MESSAGE" value="修正项目结构" />
</component>
</project>

View File

@ -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>

View File

@ -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)

View File

@ -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>

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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());
}
}

View File

@ -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>

View File

@ -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>

View File

@ -4,9 +4,9 @@ This module contains articles about conversions among Collection types and array
### Relevant Articles:
- [Array to String Conversions](https://www.baeldung.com/java-array-to-string)
- [Java Array 和 String 的转换](https://www.ossez.com/t/java-array-string/13685)
- [Mapping Lists with ModelMapper](https://www.baeldung.com/java-modelmapper-lists)
- [Converting List to Map With a Custom Supplier](https://www.baeldung.com/list-to-map-supplier)
- [Arrays.asList vs new ArrayList(Arrays.asList())](https://www.baeldung.com/java-arrays-aslist-vs-new-arraylist)
- [Java Arrays.asList 和 new ArrayList(Arrays.asList()) 的对比](https://www.ossez.com/t/java-arrays-aslist-new-arraylist-arrays-aslist/13680)
- [Iterate Over a Set in Java](https://www.baeldung.com/java-iterate-set)
- More articles: [[<-- prev]](../java-collections-conversions)

View File

@ -34,7 +34,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${org.slf4j.version}</version>
<version>${version.slf4j}</version>
</dependency>
</dependencies>

13
pom.xml
View File

@ -51,6 +51,7 @@
<!-- Logging -->
<slf4j.version>1.7.30</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<log4j2.version>2.2</log4j2.version>
<!-- APACHE COMMONS-->
@ -71,6 +72,9 @@
<ehcache.version>2.6.8</ehcache.version>
<hsqldb.version>2.3.2</hsqldb.version>
<!-- DATA STRUCTURES -->
<jackson.version>2.12.4</jackson.version>
<!-- Test -->
<junit.version>4.11</junit.version>
<hamcrest.version>2.2</hamcrest.version>
@ -79,7 +83,12 @@
<!-- CDN & CLOUD -->
<jclouds.version>1.9.0</jclouds.version>
<modelmapper.version>2.4.4</modelmapper.version>
<lombok.version>1.18.20</lombok.version>
<moneta.version>1.3</moneta.version>
<jmh-core.version>1.33</jmh-core.version>
<exec-maven-plugin.version>3.0.0</exec-maven-plugin.version>
<maven-surefire-plugin.version>2.21.0</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
</properties>
@ -118,6 +127,8 @@
<version>2.7.6</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>

View File

@ -44,6 +44,7 @@ public class VariableTest extends TestCase {
OssezVariable objB = new OssezVariable(4, 5, 6);
logger.debug("s1/s2/s3 - [{}]/[{}]/[{}]", objA.s1, objA.s2, OssezVariable.s3);
logger.debug("s1/s2/s3 - [{}]/[{}]/[{}]", objB.s1, objB.s2, OssezVariable.s3);
}
}