Skip to content

Commit 36dd488

Browse files
authored
Merge pull request eugenp#4538 from asturcon/tutorials/dateWithoutTime
BAEL-1894 - Date without time
2 parents 08b589e + 6f125f2 commit 36dd488

2 files changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.date;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.time.LocalDate;
6+
import java.util.Calendar;
7+
import java.util.Date;
8+
9+
public class DateWithoutTime {
10+
11+
public static Date getDateWithoutTimeUsingCalendar() {
12+
Calendar calendar = Calendar.getInstance();
13+
calendar.set(Calendar.HOUR_OF_DAY, 0);
14+
calendar.set(Calendar.MINUTE, 0);
15+
calendar.set(Calendar.SECOND, 0);
16+
calendar.set(Calendar.MILLISECOND, 0);
17+
18+
return calendar.getTime();
19+
}
20+
21+
public static Date getDateWithoutTimeUsingFormat() throws ParseException {
22+
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
23+
return formatter.parse(formatter.format(new Date()));
24+
}
25+
26+
public static LocalDate getLocalDate() {
27+
return LocalDate.now();
28+
}
29+
30+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)