Merge pull request #4538 from asturcon/tutorials/dateWithoutTime

BAEL-1894 - Date without time
This commit is contained in:
Loredana Crusoveanu 2018-06-26 21:14:15 +03:00 committed by GitHub
commit 36dd4888f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
public class DateWithoutTime {
public static Date getDateWithoutTimeUsingCalendar() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
}
public static Date getDateWithoutTimeUsingFormat() throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
return formatter.parse(formatter.format(new Date()));
}
public static LocalDate getLocalDate() {
return LocalDate.now();
}
}

View File

@ -0,0 +1,92 @@
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;
import static org.junit.Assert.assertNotEquals;
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));
// 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));
}
@Test
public void whenGettingDateWithoutTimeUsingFormat_thenReturnDateWithoutTime() throws ParseException {
Date dateWithoutTime = DateWithoutTime.getDateWithoutTimeUsingFormat();
// 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));
// 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));
}
@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();
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));
// 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));
}
}