|
| 1 | +package com.baeldung.date; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.text.ParseException; |
| 7 | +import java.time.LocalDate; |
| 8 | +import java.time.OffsetDateTime; |
| 9 | +import java.time.temporal.TemporalField; |
| 10 | +import java.util.Calendar; |
| 11 | +import java.util.Date; |
| 12 | + |
| 13 | +import static org.junit.Assert.assertEquals; |
| 14 | +import static org.junit.Assert.assertTrue; |
| 15 | + |
| 16 | +public class DateWithoutTimeUnitTest { |
| 17 | + |
| 18 | + private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; |
| 19 | + |
| 20 | + @Test |
| 21 | + public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() { |
| 22 | + Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar(); |
| 23 | + |
| 24 | + // first check the time is set to 0 |
| 25 | + Calendar calendar = Calendar.getInstance(); |
| 26 | + calendar.setTime(dateWithoutTime); |
| 27 | + |
| 28 | + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); |
| 29 | + assertEquals(0, calendar.get(Calendar.MINUTE)); |
| 30 | + assertEquals(0, calendar.get(Calendar.SECOND)); |
| 31 | + assertEquals(0, calendar.get(Calendar.MILLISECOND)); |
| 32 | + |
| 33 | + // now check the difference with the current Date with time is less than a day. |
| 34 | + assertTrue(new Date().getTime() - dateWithoutTime.getTime() < MILLISECONDS_PER_DAY); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() { |
| 39 | + Date dateWithoutTime = null; |
| 40 | + try { |
| 41 | + dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat(); |
| 42 | + } catch (ParseException e) { |
| 43 | + Assert.fail(); |
| 44 | + } |
| 45 | + |
| 46 | + // first check the time is set to 0 |
| 47 | + Calendar calendar = Calendar.getInstance(); |
| 48 | + calendar.setTime(dateWithoutTime); |
| 49 | + |
| 50 | + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); |
| 51 | + assertEquals(0, calendar.get(Calendar.MINUTE)); |
| 52 | + assertEquals(0, calendar.get(Calendar.SECOND)); |
| 53 | + assertEquals(0, calendar.get(Calendar.MILLISECOND)); |
| 54 | + |
| 55 | + // now check the difference with the current Date with time is less than a day. |
| 56 | + assertTrue(new Date().getTime() - dateWithoutTime.getTime() < MILLISECONDS_PER_DAY); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void whenGettingLocalDate_thenReturnDateWithoutTime() { |
| 61 | + // get the local date |
| 62 | + LocalDate localDate = DateWithoutTime.getLocalDate(); |
| 63 | + |
| 64 | + // get the millis of our LocalDate |
| 65 | + long millisLocalDate = localDate |
| 66 | + .atStartOfDay() |
| 67 | + .toInstant(OffsetDateTime |
| 68 | + .now() |
| 69 | + .getOffset()) |
| 70 | + .toEpochMilli(); |
| 71 | + |
| 72 | + |
| 73 | + // get current millis from Date with time |
| 74 | + long millisDate = new Date().getTime(); |
| 75 | + |
| 76 | + // the difference in time has to be less than a day |
| 77 | + assertTrue(millisDate - millisLocalDate < MILLISECONDS_PER_DAY); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments