From 914859d629be6ce07b15bc7e8747bc2a6beaa2a2 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Sun, 24 Jun 2018 12:15:09 +0200 Subject: [PATCH] Date without time --- .../com/baeldung/date/DateWithoutTime.java | 30 +++++++ .../date/DateWithoutTimeUnitTest.java | 80 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/date/DateWithoutTime.java create mode 100644 core-java/src/test/java/com/baeldung/date/DateWithoutTimeUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/date/DateWithoutTime.java b/core-java/src/main/java/com/baeldung/date/DateWithoutTime.java new file mode 100644 index 0000000000..fed9141597 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/date/DateWithoutTime.java @@ -0,0 +1,30 @@ +package com.baeldung.date; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.util.Calendar; +import java.util.Date; + +public class DateWithoutTime { + + public static Date getDateWithoutTimeUsingCalendar() { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + + return calendar.getTime(); + } + + public static Date getDateWithoutTimeUsingFormat() throws ParseException { + SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); + return formatter.parse(formatter.format(new Date())); + } + + public static LocalDate getLocalDate() { + return LocalDate.now(); + } + +} diff --git a/core-java/src/test/java/com/baeldung/date/DateWithoutTimeUnitTest.java b/core-java/src/test/java/com/baeldung/date/DateWithoutTimeUnitTest.java new file mode 100644 index 0000000000..f8686f4823 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/date/DateWithoutTimeUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.date; + +import org.junit.Assert; +import org.junit.Test; + +import java.text.ParseException; +import java.time.LocalDate; +import java.time.OffsetDateTime; +import java.time.temporal.TemporalField; +import java.util.Calendar; +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class DateWithoutTimeUnitTest { + + private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; + + @Test + public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() { + Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar(); + + // first check the time is set to 0 + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateWithoutTime); + + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + assertEquals(0, calendar.get(Calendar.MILLISECOND)); + + // now check the difference with the current Date with time is less than a day. + assertTrue(new Date().getTime() - dateWithoutTime.getTime() < MILLISECONDS_PER_DAY); + } + + @Test + public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() { + Date dateWithoutTime = null; + try { + dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat(); + } catch (ParseException e) { + Assert.fail(); + } + + // first check the time is set to 0 + Calendar calendar = Calendar.getInstance(); + calendar.setTime(dateWithoutTime); + + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); + assertEquals(0, calendar.get(Calendar.MINUTE)); + assertEquals(0, calendar.get(Calendar.SECOND)); + assertEquals(0, calendar.get(Calendar.MILLISECOND)); + + // now check the difference with the current Date with time is less than a day. + assertTrue(new Date().getTime() - dateWithoutTime.getTime() < MILLISECONDS_PER_DAY); + } + + @Test + public void whenGettingLocalDate_thenReturnDateWithoutTime() { + // get the local date + LocalDate localDate = DateWithoutTime.getLocalDate(); + + // get the millis of our LocalDate + long millisLocalDate = localDate + .atStartOfDay() + .toInstant(OffsetDateTime + .now() + .getOffset()) + .toEpochMilli(); + + + // get current millis from Date with time + long millisDate = new Date().getTime(); + + // the difference in time has to be less than a day + assertTrue(millisDate - millisLocalDate < MILLISECONDS_PER_DAY); + } + +}