* Added code for BAEL-1899 get start and end of a day (#4567)

This commit is contained in:
rozagerardo 2018-06-26 02:30:31 -03:00 committed by maibin
parent 1609a9a5de
commit 7e0d553340
6 changed files with 144 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package com.baeldung.datetime;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
@ -43,4 +44,30 @@ class UseLocalDate {
LocalDateTime startofDay = localDate.atStartOfDay();
return startofDay;
}
LocalDateTime getStartOfDayOfLocalDate(LocalDate localDate) {
LocalDateTime startofDay = LocalDateTime.of(localDate, LocalTime.MIDNIGHT);
return startofDay;
}
LocalDateTime getStartOfDayAtMinTime(LocalDate localDate) {
LocalDateTime startofDay = localDate.atTime(LocalTime.MIN);
return startofDay;
}
LocalDateTime getStartOfDayAtMidnightTime(LocalDate localDate) {
LocalDateTime startofDay = localDate.atTime(LocalTime.MIDNIGHT);
return startofDay;
}
LocalDateTime getEndOfDay(LocalDate localDate) {
LocalDateTime endOfDay = localDate.atTime(LocalTime.MAX);
return endOfDay;
}
LocalDateTime getEndOfDayFromLocalTime(LocalDate localDate) {
LocalDateTime endOfDate = LocalTime.MAX.atDate(localDate);
return endOfDate;
}
}

View File

@ -1,6 +1,8 @@
package com.baeldung.datetime;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoField;
public class UseLocalDateTime {
@ -8,4 +10,15 @@ public class UseLocalDateTime {
return LocalDateTime.parse(representation);
}
LocalDateTime getEndOfDayFromLocalDateTimeDirectly(LocalDateTime localDateTime) {
LocalDateTime endOfDate = localDateTime.with(ChronoField.NANO_OF_DAY, LocalTime.MAX.toNanoOfDay());
return endOfDate;
}
LocalDateTime getEndOfDayFromLocalDateTime(LocalDateTime localDateTime) {
LocalDateTime endOfDate = localDateTime.toLocalDate()
.atTime(LocalTime.MAX);
return endOfDate;
}
}

View File

@ -1,12 +1,42 @@
package com.baeldung.datetime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
class UseZonedDateTime {
ZonedDateTime getZonedDateTime(LocalDateTime localDateTime, ZoneId zoneId) {
return ZonedDateTime.of(localDateTime, zoneId);
}
ZonedDateTime getStartOfDay(LocalDate localDate, ZoneId zone) {
ZonedDateTime startofDay = localDate.atStartOfDay()
.atZone(zone);
return startofDay;
}
ZonedDateTime getStartOfDayShorthand(LocalDate localDate, ZoneId zone) {
ZonedDateTime startofDay = localDate.atStartOfDay(zone);
return startofDay;
}
ZonedDateTime getStartOfDayFromZonedDateTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startofDay = zonedDateTime.toLocalDateTime()
.toLocalDate()
.atStartOfDay(zonedDateTime.getZone());
return startofDay;
}
ZonedDateTime getStartOfDayAtMinTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startofDay = zonedDateTime.with(ChronoField.HOUR_OF_DAY, 0);
return startofDay;
}
ZonedDateTime getStartOfDayAtMidnightTime(ZonedDateTime zonedDateTime) {
ZonedDateTime startofDay = zonedDateTime.with(ChronoField.NANO_OF_DAY, 0);
return startofDay;
}
}

View File

@ -1,13 +1,15 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UseLocalDateTimeUnitTest {
UseLocalDateTime useLocalDateTime = new UseLocalDateTime();
@ -19,4 +21,16 @@ public class UseLocalDateTimeUnitTest {
assertEquals(LocalTime.of(6, 30), useLocalDateTime.getLocalDateTimeUsingParseMethod("2016-05-10T06:30")
.toLocalTime());
}
@Test
public void givenLocalDateTime_whenSettingEndOfDay_thenReturnLastMomentOfDay() {
LocalDateTime givenTimed = LocalDateTime.parse("2018-06-23T05:55:55");
LocalDateTime endOfDayFromGivenDirectly = useLocalDateTime.getEndOfDayFromLocalDateTimeDirectly(givenTimed);
LocalDateTime endOfDayFromGiven = useLocalDateTime.getEndOfDayFromLocalDateTime(givenTimed);
assertThat(endOfDayFromGivenDirectly).isEqualTo(endOfDayFromGiven);
assertThat(endOfDayFromGivenDirectly.toLocalTime()).isEqualTo(LocalTime.MAX);
assertThat(endOfDayFromGivenDirectly.toString()).isEqualTo("2018-06-23T23:59:59.999999999");
}
}

View File

@ -1,13 +1,15 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UseLocalDateUnitTest {
UseLocalDate useLocalDate = new UseLocalDate();
@ -57,4 +59,33 @@ public class UseLocalDateUnitTest {
assertEquals(LocalDateTime.parse("2016-05-22T00:00:00"), useLocalDate.getStartOfDay(LocalDate.parse("2016-05-22")));
}
@Test
public void givenLocalDate_whenSettingStartOfDay_thenReturnMidnightInAllCases() {
LocalDate given = LocalDate.parse("2018-06-23");
LocalDateTime startOfDayWithMethod = useLocalDate.getStartOfDay(given);
LocalDateTime startOfDayOfLocalDate = useLocalDate.getStartOfDayOfLocalDate(given);
LocalDateTime startOfDayWithMin = useLocalDate.getStartOfDayAtMinTime(given);
LocalDateTime startOfDayWithMidnight = useLocalDate.getStartOfDayAtMidnightTime(given);
assertThat(startOfDayWithMethod).isEqualTo(startOfDayWithMin)
.isEqualTo(startOfDayWithMidnight)
.isEqualTo(startOfDayOfLocalDate)
.isEqualTo(LocalDateTime.parse("2018-06-23T00:00:00"));
assertThat(startOfDayWithMin.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT);
assertThat(startOfDayWithMin.toString()).isEqualTo("2018-06-23T00:00");
}
@Test
public void givenLocalDate_whenSettingEndOfDay_thenReturnLastMomentOfDay() {
LocalDate given = LocalDate.parse("2018-06-23");
LocalDateTime endOfDayWithMax = useLocalDate.getEndOfDay(given);
LocalDateTime endOfDayFromLocalTime = useLocalDate.getEndOfDayFromLocalTime(given);
assertThat(endOfDayWithMax).isEqualTo(endOfDayFromLocalTime);
assertThat(endOfDayWithMax.toLocalTime()).isEqualTo(LocalTime.MAX);
assertThat(endOfDayWithMax.toString()).isEqualTo("2018-06-23T23:59:59.999999999");
}
}

View File

@ -1,6 +1,10 @@
package com.baeldung.datetime;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@ -17,4 +21,25 @@ public class UseZonedDateTimeUnitTest {
ZonedDateTime zonedDatetime = zonedDateTime.getZonedDateTime(LocalDateTime.parse("2016-05-20T06:30"), zoneId);
Assert.assertEquals(zoneId, ZoneId.from(zonedDatetime));
}
@Test
public void givenLocalDateOrZoned_whenSettingStartOfDay_thenReturnMidnightInAllCases() {
LocalDate given = LocalDate.parse("2018-06-23");
ZoneId zone = ZoneId.of("Europe/Paris");
ZonedDateTime zonedGiven = ZonedDateTime.of(given, LocalTime.NOON, zone);
ZonedDateTime startOfOfDayWithMethod = zonedDateTime.getStartOfDay(given, zone);
ZonedDateTime startOfOfDayWithShorthandMethod = zonedDateTime.getStartOfDayShorthand(given, zone);
ZonedDateTime startOfOfDayFromZonedDateTime = zonedDateTime.getStartOfDayFromZonedDateTime(zonedGiven);
ZonedDateTime startOfOfDayAtMinTime = zonedDateTime.getStartOfDayAtMinTime(zonedGiven);
ZonedDateTime startOfOfDayAtMidnightTime = zonedDateTime.getStartOfDayAtMidnightTime(zonedGiven);
assertThat(startOfOfDayWithMethod).isEqualTo(startOfOfDayWithShorthandMethod)
.isEqualTo(startOfOfDayFromZonedDateTime)
.isEqualTo(startOfOfDayAtMinTime)
.isEqualTo(startOfOfDayAtMidnightTime);
assertThat(startOfOfDayWithMethod.toLocalTime()).isEqualTo(LocalTime.MIDNIGHT);
assertThat(startOfOfDayWithMethod.toLocalTime()
.toString()).isEqualTo("00:00");
}
}