66import java .text .ParseException ;
77import java .time .LocalDate ;
88import java .time .OffsetDateTime ;
9- import java .time .temporal .TemporalField ;
109import java .util .Calendar ;
1110import java .util .Date ;
1211
1312import static org .junit .Assert .assertEquals ;
14- import static org .junit .Assert .assertTrue ;
13+ import static org .junit .Assert .assertNotEquals ;
1514
1615public class DateWithoutTimeUnitTest {
1716
@@ -30,18 +29,21 @@ public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime()
3029 assertEquals (0 , calendar .get (Calendar .SECOND ));
3130 assertEquals (0 , calendar .get (Calendar .MILLISECOND ));
3231
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 );
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 ));
3542 }
3643
3744 @ 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+ public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime () throws ParseException {
46+ Date dateWithoutTime = DateWithoutTime .getDateWithoutTimeUsingFormat ();
4547
4648 // first check the time is set to 0
4749 Calendar calendar = Calendar .getInstance ();
@@ -52,8 +54,16 @@ public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() {
5254 assertEquals (0 , calendar .get (Calendar .SECOND ));
5355 assertEquals (0 , calendar .get (Calendar .MILLISECOND ));
5456
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+ // 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 ));
5767 }
5868
5969 @ Test
@@ -69,12 +79,14 @@ public void whenGettingLocalDate_thenReturnDateWithoutTime() {
6979 .getOffset ())
7080 .toEpochMilli ();
7181
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 ));
7286
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 );
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 ));
7890 }
7991
8092}
0 commit comments