Skip to content

Commit 914859d

Browse files
committed
Date without time
1 parent ca5d22f commit 914859d

2 files changed

Lines changed: 110 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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)