|
| 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.util.Calendar; |
| 10 | +import java.util.Date; |
| 11 | + |
| 12 | +import static org.junit.Assert.assertEquals; |
| 13 | +import static org.junit.Assert.assertNotEquals; |
| 14 | + |
| 15 | +public class DateWithoutTimeUnitTest { |
| 16 | + |
| 17 | + private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000; |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() { |
| 21 | + Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar(); |
| 22 | + |
| 23 | + // first check the time is set to 0 |
| 24 | + Calendar calendar = Calendar.getInstance(); |
| 25 | + calendar.setTime(dateWithoutTime); |
| 26 | + |
| 27 | + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); |
| 28 | + assertEquals(0, calendar.get(Calendar.MINUTE)); |
| 29 | + assertEquals(0, calendar.get(Calendar.SECOND)); |
| 30 | + assertEquals(0, calendar.get(Calendar.MILLISECOND)); |
| 31 | + |
| 32 | + // we get the day of the date |
| 33 | + int day = calendar.get(Calendar.DAY_OF_MONTH); |
| 34 | + |
| 35 | + // if we add the mills of one day minus 1 we should get the same day |
| 36 | + calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1); |
| 37 | + assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH)); |
| 38 | + |
| 39 | + // if we add one full day in millis we should get a different day |
| 40 | + calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY); |
| 41 | + assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH)); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() throws ParseException { |
| 46 | + Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat(); |
| 47 | + |
| 48 | + // first check the time is set to 0 |
| 49 | + Calendar calendar = Calendar.getInstance(); |
| 50 | + calendar.setTime(dateWithoutTime); |
| 51 | + |
| 52 | + assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY)); |
| 53 | + assertEquals(0, calendar.get(Calendar.MINUTE)); |
| 54 | + assertEquals(0, calendar.get(Calendar.SECOND)); |
| 55 | + assertEquals(0, calendar.get(Calendar.MILLISECOND)); |
| 56 | + |
| 57 | + // we get the day of the date |
| 58 | + int day = calendar.get(Calendar.DAY_OF_MONTH); |
| 59 | + |
| 60 | + // if we add the mills of one day minus 1 we should get the same day |
| 61 | + calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1); |
| 62 | + assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH)); |
| 63 | + |
| 64 | + // if we add one full day in millis we should get a different day |
| 65 | + calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY); |
| 66 | + assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void whenGettingLocalDate_thenReturnDateWithoutTime() { |
| 71 | + // get the local date |
| 72 | + LocalDate localDate = DateWithoutTime.getLocalDate(); |
| 73 | + |
| 74 | + // get the millis of our LocalDate |
| 75 | + long millisLocalDate = localDate |
| 76 | + .atStartOfDay() |
| 77 | + .toInstant(OffsetDateTime |
| 78 | + .now() |
| 79 | + .getOffset()) |
| 80 | + .toEpochMilli(); |
| 81 | + |
| 82 | + Calendar calendar = Calendar.getInstance(); |
| 83 | + // if we add the millis of one day minus 1 we should get the same day |
| 84 | + calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY - 1); |
| 85 | + assertEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH)); |
| 86 | + |
| 87 | + // if we add one full day in millis we should get a different day |
| 88 | + calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY); |
| 89 | + assertNotEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH)); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments