* BAEL-1344 add java8 convert methods date, localdate, localdatetime

* BAEL-1344 add java9 example
This commit is contained in:
abialas 2017-11-19 22:03:09 +01:00 committed by maibin
parent e2202f0285
commit 6211154062
12 changed files with 519 additions and 0 deletions

View File

@ -0,0 +1,35 @@
/**
*
*/
package com.baeldung.datetime;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
/**
* Class which shows different ways of converting 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();
}
}

View File

@ -0,0 +1,35 @@
/**
*
*/
package com.baeldung.datetime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
/**
* Class which shows different ways of converting 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();
}
}

View File

@ -0,0 +1,27 @@
/**
*
*/
package com.baeldung.datetime;
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.datetime;
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,72 @@
/**
*
*/
package com.baeldung.datetime;
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;
/**
*
* JUnits for {@link DateToLocalDateConverter} class.
*
* @author abialas
*
*/
public class DateToLocalDateConverterTest {
@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));
}
}

View File

@ -0,0 +1,78 @@
/**
*
*/
package com.baeldung.datetime;
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;
/**
*
* JUnits for {@link DateToLocalDateTimeConverter} class.
*
* @author abialas
*
*/
public class DateToLocalDateTimeConverterTest {
@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));
}
}

View File

@ -0,0 +1,61 @@
/**
*
*/
package com.baeldung.datetime;
import static org.junit.Assert.assertEquals;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
/**
*
* JUnits for {@link LocalDateTimeToDateConverter} class.
*
* @author abialas
*
*/
public class LocalDateTimeToDateConverterTest {
@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,55 @@
/**
*
*/
package com.baeldung.datetime;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
/**
*
* JUnits for {@link LocalDateToDateConverter} class.
*
* @author abialas
*
*/
public class LocalDateToDateConverterTest {
@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,22 @@
/**
*
*/
package com.baeldung.java9.datetime;
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 with new Java 1.9.
*
* @author abialas
*
*/
public class DateToLocalDateConverter {
public static LocalDate convertToLocalDate(Date dateToConvert) {
return LocalDate.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
}
}

View File

@ -0,0 +1,22 @@
/**
*
*/
package com.baeldung.java9.datetime;
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 with new Java 1.9.
*
* @author abialas
*
*/
public class DateToLocalDateTimeConverter {
public static LocalDateTime convertToLocalDateTime(Date dateToConvert) {
return LocalDateTime.ofInstant(dateToConvert.toInstant(), ZoneId.systemDefault());
}
}

View File

@ -0,0 +1,41 @@
/**
*
*/
package com.baeldung.java9.datetime;
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.java9.datetime.DateToLocalDateConverter;
/**
* JUnits for {@link DateToLocalDateConverter} class.
*
* @author abialas
*
*/
public class DateToLocalDateConverterTest {
@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,43 @@
/**
*
*/
package com.baeldung.java9.datetime;
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.java9.datetime.DateToLocalDateTimeConverter;
/**
* JUnits for {@link DateToLocalDateTimeConverter} class.
*
* @author abialas
*
*/
public class DateToLocalDateTimeConverterTest {
@Test
public void shouldReturn10thNovember2010time8hour20minWhenConvertViaInstant() {
// 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));
}
}