2018-06-24 12:15:09 +02:00
|
|
|
package com.baeldung.date;
|
|
|
|
|
|
|
|
|
|
import org.junit.Assert;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.OffsetDateTime;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
2018-06-24 20:23:43 +02:00
|
|
|
import static org.junit.Assert.assertNotEquals;
|
2018-06-24 12:15:09 +02:00
|
|
|
|
|
|
|
|
public class DateWithoutTimeUnitTest {
|
|
|
|
|
|
|
|
|
|
private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenGettingDateWithoutTimeUsingCalendar_thenReturnDateWithoutTime() {
|
|
|
|
|
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingCalendar();
|
|
|
|
|
|
|
|
|
|
// first check the time is set to 0
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.setTime(dateWithoutTime);
|
|
|
|
|
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.MINUTE));
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.SECOND));
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
|
|
|
|
|
2018-06-24 20:23:43 +02:00
|
|
|
// we get the day of the date
|
|
|
|
|
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
|
|
// if we add the mills of one day minus 1 we should get the same day
|
|
|
|
|
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
|
|
|
|
|
assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
|
|
|
|
|
|
|
|
|
// if we add one full day in millis we should get a different day
|
|
|
|
|
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
|
|
|
|
|
assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
2018-06-24 12:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2018-06-24 20:23:43 +02:00
|
|
|
public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() throws ParseException {
|
|
|
|
|
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat();
|
2018-06-24 12:15:09 +02:00
|
|
|
|
|
|
|
|
// first check the time is set to 0
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
calendar.setTime(dateWithoutTime);
|
|
|
|
|
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.HOUR_OF_DAY));
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.MINUTE));
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.SECOND));
|
|
|
|
|
assertEquals(0, calendar.get(Calendar.MILLISECOND));
|
|
|
|
|
|
2018-06-24 20:23:43 +02:00
|
|
|
// we get the day of the date
|
|
|
|
|
int day = calendar.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
|
|
// if we add the mills of one day minus 1 we should get the same day
|
|
|
|
|
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY - 1);
|
|
|
|
|
assertEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
|
|
|
|
|
|
|
|
|
// if we add one full day in millis we should get a different day
|
|
|
|
|
calendar.setTimeInMillis(dateWithoutTime.getTime() + MILLISECONDS_PER_DAY);
|
|
|
|
|
assertNotEquals(day, calendar.get(Calendar.DAY_OF_MONTH));
|
2018-06-24 12:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void whenGettingLocalDate_thenReturnDateWithoutTime() {
|
|
|
|
|
// get the local date
|
|
|
|
|
LocalDate localDate = DateWithoutTime.getLocalDate();
|
|
|
|
|
|
|
|
|
|
// get the millis of our LocalDate
|
|
|
|
|
long millisLocalDate = localDate
|
|
|
|
|
.atStartOfDay()
|
|
|
|
|
.toInstant(OffsetDateTime
|
|
|
|
|
.now()
|
|
|
|
|
.getOffset())
|
|
|
|
|
.toEpochMilli();
|
|
|
|
|
|
2018-06-24 20:23:43 +02:00
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
// if we add the millis of one day minus 1 we should get the same day
|
|
|
|
|
calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY - 1);
|
|
|
|
|
assertEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH));
|
2018-06-24 12:15:09 +02:00
|
|
|
|
2018-06-24 20:23:43 +02:00
|
|
|
// if we add one full day in millis we should get a different day
|
|
|
|
|
calendar.setTimeInMillis(millisLocalDate + MILLISECONDS_PER_DAY);
|
|
|
|
|
assertNotEquals(localDate.getDayOfMonth(), calendar.get(Calendar.DAY_OF_MONTH));
|
2018-06-24 12:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|