Merge pull request #430 from sivabalachandran/java8DateTime

Java8 date time
This commit is contained in:
Alex Theedom 2016-06-07 00:31:28 +03:00
commit 49c9072564
12 changed files with 314 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.baeldung.datetime;
import java.time.Duration;
import java.time.LocalTime;
import java.time.Period;
public class UseDuration {
public LocalTime modifyDates(LocalTime localTime,Duration duration){
return localTime.plus(duration);
}
public Duration getDifferenceBetweenDates(LocalTime localTime1,LocalTime localTime2){
return Duration.between(localTime1, localTime2);
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
public class UseLocalDate {
public LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth){
return LocalDate.of(year, month, dayOfMonth);
}
public LocalDate getLocalDateUsingParseMethod(String representation){
return LocalDate.parse(representation);
}
public LocalDate getLocalDateFromClock(){
LocalDate localDate = LocalDate.now();
return localDate;
}
public LocalDate getNextDay(LocalDate localDate){
return localDate.plusDays(1);
}
public LocalDate getPreviousDay(LocalDate localDate){
return localDate.minus(1, ChronoUnit.DAYS);
}
public DayOfWeek getDayOfWeek(LocalDate localDate){
DayOfWeek day = localDate.getDayOfWeek();
return day;
}
public LocalDate getFirstDayOfMonth(){
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
return firstDayOfMonth;
}
public LocalDateTime getStartOfDay(LocalDate localDate){
LocalDateTime startofDay = localDate.atStartOfDay();
return startofDay;
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
public class UseLocalDateTime {
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation){
return LocalDateTime.parse(representation);
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.datetime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class UseLocalTime {
public LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds){
LocalTime localTime = LocalTime.of(hour, min, seconds);
return localTime;
}
public LocalTime getLocalTimeUsingParseMethod(String timeRepresentation){
LocalTime localTime = LocalTime.parse(timeRepresentation);
return localTime;
}
public LocalTime getLocalTimeFromClock(){
LocalTime localTime = LocalTime.now();
return localTime;
}
public LocalTime addAnHour(LocalTime localTime){
LocalTime newTime = localTime.plus(1,ChronoUnit.HOURS);
return newTime;
}
public int getHourFromLocalTime(LocalTime localTime){
return localTime.getHour();
}
public LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute){
return localTime.withMinute(minute);
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.Period;
public class UsePeriod {
public LocalDate modifyDates(LocalDate localDate,Period period){
return localDate.plus(period);
}
public Period getDifferenceBetweenDates(LocalDate localDate1,LocalDate localDate2){
return Period.between(localDate1, localDate2);
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
public class UseToInstant {
public LocalDateTime convertDateToLocalDate(Date date){
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
return localDateTime;
}
public LocalDateTime convertDateToLocalDate(Calendar calendar){
LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
return localDateTime;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class UseZonedDateTime {
public ZonedDateTime getZonedDateTime(LocalDateTime localDateTime,ZoneId zoneId){
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
return zonedDateTime;
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import org.junit.Assert;
import org.junit.Test;
public class UseLocalDateTest {
UseLocalDate useLocalDate = new UseLocalDate();
@Test
public void givenValues_whenUsingFactoryOf_thenLocalDate(){
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingFactoryOfMethod(2016,5,10).toString());
}
@Test
public void givenString_whenUsingParse_thenLocalDate(){
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString());
}
@Test
public void whenUsingClock_thenLocalDate(){
Assert.assertEquals(LocalDate.now(),useLocalDate.getLocalDateFromClock());
}
@Test
public void givenDate_whenUsingPlus_thenNextDay(){
Assert.assertEquals(LocalDate.now().plusDays(1),useLocalDate.getNextDay(LocalDate.now()));
}
@Test
public void givenDate_whenUsingMinus_thenPreviousDay(){
Assert.assertEquals(LocalDate.now().minusDays(1),useLocalDate.getPreviousDay(LocalDate.now()));
}
@Test
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek(){
Assert.assertEquals(DayOfWeek.SUNDAY,useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
}
@Test
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth(){
Assert.assertEquals(1,useLocalDate.getFirstDayOfMonth().getDayOfMonth());
}
@Test
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight(){
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"),useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import org.junit.Assert;
import org.junit.Test;
public class UseLocalDateTimeTest {
UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
@Test
public void givenString_whenUsingParse_thenLocalDateTime(){
Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate());
Assert.assertEquals(LocalTime.of(6,30),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime());
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.datetime;
import java.time.LocalTime;
import org.junit.Assert;
import org.junit.Test;
public class UseLocalTimeTest {
UseLocalTime useLocalTime = new UseLocalTime();
@Test
public void givenValues_whenUsingFactoryOf_thenLocalTime(){
Assert.assertEquals("07:07:07",useLocalTime.getLocalTimeUsingFactoryOfMethod(7,7,7).toString());
}
@Test
public void givenString_whenUsingParse_thenLocalTime(){
Assert.assertEquals("06:30",useLocalTime.getLocalTimeUsingParseMethod("06:30").toString());
}
@Test
public void givenTime_whenAddHour_thenLocalTime(){
Assert.assertEquals("07:30",useLocalTime.addAnHour(LocalTime.of(6,30)).toString());
}
@Test
public void getHourFromLocalTime(){
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1,1)));
}
@Test
public void getLocalTimeWithMinuteSetToValue(){
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10,10), 20));
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Assert;
import org.junit.Test;
public class UsePeriodTest {
UsePeriod usingPeriod=new UsePeriod();
@Test
public void givenPeriodAndLocalDate_thenCalculateModifiedDate(){
Period period = Period.ofDays(1);
LocalDate localDate = LocalDate.parse("2007-05-10");
Assert.assertEquals(localDate.plusDays(1),usingPeriod.modifyDates(localDate, period));
}
@Test
public void givenDates_thenGetPeriod(){
LocalDate localDate1 = LocalDate.parse("2007-05-10");
LocalDate localDate2 = LocalDate.parse("2007-05-15");
Assert.assertEquals(Period.ofDays(5), usingPeriod.getDifferenceBetweenDates(localDate1, localDate2));
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.junit.Assert;
import org.junit.Test;
public class UseZonedDateTimeTest {
UseZonedDateTime zonedDateTime=new UseZonedDateTime();
@Test
public void givenZoneId_thenZonedDateTime(){
ZoneId zoneId=ZoneId.of("Europe/Paris");
ZonedDateTime zonedDatetime=zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
Assert.assertEquals(zoneId,ZoneId.from(zonedDatetime));
}
}