Skip to content

Commit 49c9072

Browse files
committed
Merge pull request eugenp#430 from sivabalachandran/java8DateTime
Java8 date time
2 parents 36af7ed + 0a3f723 commit 49c9072

12 files changed

Lines changed: 314 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.Duration;
4+
import java.time.LocalTime;
5+
import java.time.Period;
6+
7+
public class UseDuration {
8+
9+
public LocalTime modifyDates(LocalTime localTime,Duration duration){
10+
return localTime.plus(duration);
11+
}
12+
13+
public Duration getDifferenceBetweenDates(LocalTime localTime1,LocalTime localTime2){
14+
return Duration.between(localTime1, localTime2);
15+
}
16+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.DayOfWeek;
4+
import java.time.LocalDate;
5+
import java.time.LocalDateTime;
6+
import java.time.temporal.ChronoUnit;
7+
import java.time.temporal.TemporalAdjusters;
8+
9+
public class UseLocalDate {
10+
11+
public LocalDate getLocalDateUsingFactoryOfMethod(int year, int month, int dayOfMonth){
12+
return LocalDate.of(year, month, dayOfMonth);
13+
}
14+
15+
public LocalDate getLocalDateUsingParseMethod(String representation){
16+
return LocalDate.parse(representation);
17+
}
18+
19+
public LocalDate getLocalDateFromClock(){
20+
LocalDate localDate = LocalDate.now();
21+
return localDate;
22+
}
23+
24+
public LocalDate getNextDay(LocalDate localDate){
25+
return localDate.plusDays(1);
26+
}
27+
28+
public LocalDate getPreviousDay(LocalDate localDate){
29+
return localDate.minus(1, ChronoUnit.DAYS);
30+
}
31+
32+
public DayOfWeek getDayOfWeek(LocalDate localDate){
33+
DayOfWeek day = localDate.getDayOfWeek();
34+
return day;
35+
}
36+
37+
public LocalDate getFirstDayOfMonth(){
38+
LocalDate firstDayOfMonth = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
39+
return firstDayOfMonth;
40+
}
41+
42+
public LocalDateTime getStartOfDay(LocalDate localDate){
43+
LocalDateTime startofDay = localDate.atStartOfDay();
44+
return startofDay;
45+
}
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDateTime;
4+
5+
public class UseLocalDateTime {
6+
7+
public LocalDateTime getLocalDateTimeUsingParseMethod(String representation){
8+
return LocalDateTime.parse(representation);
9+
}
10+
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalTime;
4+
import java.time.temporal.ChronoUnit;
5+
6+
public class UseLocalTime {
7+
8+
public LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds){
9+
LocalTime localTime = LocalTime.of(hour, min, seconds);
10+
return localTime;
11+
}
12+
13+
public LocalTime getLocalTimeUsingParseMethod(String timeRepresentation){
14+
LocalTime localTime = LocalTime.parse(timeRepresentation);
15+
return localTime;
16+
}
17+
18+
public LocalTime getLocalTimeFromClock(){
19+
LocalTime localTime = LocalTime.now();
20+
return localTime;
21+
}
22+
23+
public LocalTime addAnHour(LocalTime localTime){
24+
LocalTime newTime = localTime.plus(1,ChronoUnit.HOURS);
25+
return newTime;
26+
}
27+
28+
public int getHourFromLocalTime(LocalTime localTime){
29+
return localTime.getHour();
30+
}
31+
32+
public LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute){
33+
return localTime.withMinute(minute);
34+
}
35+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDate;
4+
import java.time.Period;
5+
6+
public class UsePeriod {
7+
8+
public LocalDate modifyDates(LocalDate localDate,Period period){
9+
return localDate.plus(period);
10+
}
11+
12+
public Period getDifferenceBetweenDates(LocalDate localDate1,LocalDate localDate2){
13+
return Period.between(localDate1, localDate2);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.ZoneId;
5+
import java.util.Calendar;
6+
import java.util.Date;
7+
8+
public class UseToInstant {
9+
10+
public LocalDateTime convertDateToLocalDate(Date date){
11+
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
12+
return localDateTime;
13+
}
14+
15+
public LocalDateTime convertDateToLocalDate(Calendar calendar){
16+
LocalDateTime localDateTime = LocalDateTime.ofInstant(calendar.toInstant(), ZoneId.systemDefault());
17+
return localDateTime;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.ZoneId;
5+
import java.time.ZonedDateTime;
6+
7+
public class UseZonedDateTime {
8+
9+
public ZonedDateTime getZonedDateTime(LocalDateTime localDateTime,ZoneId zoneId){
10+
ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId);
11+
return zonedDateTime;
12+
}
13+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.DayOfWeek;
4+
import java.time.LocalDate;
5+
import java.time.LocalDateTime;
6+
import java.time.Month;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
public class UseLocalDateTest {
12+
13+
UseLocalDate useLocalDate = new UseLocalDate();
14+
15+
@Test
16+
public void givenValues_whenUsingFactoryOf_thenLocalDate(){
17+
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingFactoryOfMethod(2016,5,10).toString());
18+
}
19+
20+
@Test
21+
public void givenString_whenUsingParse_thenLocalDate(){
22+
Assert.assertEquals("2016-05-10",useLocalDate.getLocalDateUsingParseMethod("2016-05-10").toString());
23+
}
24+
25+
@Test
26+
public void whenUsingClock_thenLocalDate(){
27+
Assert.assertEquals(LocalDate.now(),useLocalDate.getLocalDateFromClock());
28+
}
29+
30+
@Test
31+
public void givenDate_whenUsingPlus_thenNextDay(){
32+
Assert.assertEquals(LocalDate.now().plusDays(1),useLocalDate.getNextDay(LocalDate.now()));
33+
}
34+
35+
@Test
36+
public void givenDate_whenUsingMinus_thenPreviousDay(){
37+
Assert.assertEquals(LocalDate.now().minusDays(1),useLocalDate.getPreviousDay(LocalDate.now()));
38+
}
39+
40+
@Test
41+
public void givenToday_whenUsingGetDayOfWeek_thenDayOfWeek(){
42+
Assert.assertEquals(DayOfWeek.SUNDAY,useLocalDate.getDayOfWeek(LocalDate.parse("2016-05-22")));
43+
}
44+
45+
@Test
46+
public void givenToday_whenUsingWithTemporalAdjuster_thenFirstDayOfMonth(){
47+
Assert.assertEquals(1,useLocalDate.getFirstDayOfMonth().getDayOfMonth());
48+
}
49+
50+
@Test
51+
public void givenLocalDate_whenUsingAtStartOfDay_thenReturnMidnight(){
52+
Assert.assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"),useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
53+
}
54+
55+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalDate;
4+
import java.time.LocalTime;
5+
import java.time.Month;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class UseLocalDateTimeTest {
11+
12+
UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
13+
14+
@Test
15+
public void givenString_whenUsingParse_thenLocalDateTime(){
16+
Assert.assertEquals(LocalDate.of(2016, Month.MAY, 10),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalDate());
17+
Assert.assertEquals(LocalTime.of(6,30),useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30").toLocalTime());
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.datetime;
2+
3+
import java.time.LocalTime;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class UseLocalTimeTest {
9+
10+
UseLocalTime useLocalTime = new UseLocalTime();
11+
12+
@Test
13+
public void givenValues_whenUsingFactoryOf_thenLocalTime(){
14+
Assert.assertEquals("07:07:07",useLocalTime.getLocalTimeUsingFactoryOfMethod(7,7,7).toString());
15+
}
16+
17+
@Test
18+
public void givenString_whenUsingParse_thenLocalTime(){
19+
Assert.assertEquals("06:30",useLocalTime.getLocalTimeUsingParseMethod("06:30").toString());
20+
}
21+
22+
@Test
23+
public void givenTime_whenAddHour_thenLocalTime(){
24+
Assert.assertEquals("07:30",useLocalTime.addAnHour(LocalTime.of(6,30)).toString());
25+
}
26+
27+
@Test
28+
public void getHourFromLocalTime(){
29+
Assert.assertEquals(1, useLocalTime.getHourFromLocalTime(LocalTime.of(1,1)));
30+
}
31+
32+
@Test
33+
public void getLocalTimeWithMinuteSetToValue(){
34+
Assert.assertEquals(LocalTime.of(10, 20), useLocalTime.getLocalTimeWithMinuteSetToValue(LocalTime.of(10,10), 20));
35+
}
36+
}

0 commit comments

Comments
 (0)