BAEL-3375 Working with Dates before Java 8
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sql;
|
||||
package com.baeldung.legacy.sqlpackage;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.text.ParseException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sql;
|
||||
package com.baeldung.legacy.sqlpackage;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.text.ParseException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sql;
|
||||
package com.baeldung.legacy.sqlpackage;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.legacy.utilpackage;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class CalendarUtils {
|
||||
|
||||
public static Calendar getPlusDays(Date date, int amount) throws ParseException {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(date);
|
||||
calendar.add(Calendar.DAY_OF_YEAR, amount);
|
||||
return calendar;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.legacy.utilpackage;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
public static Date getNow() {
|
||||
return new Date();
|
||||
}
|
||||
|
||||
public static Date getDate(long millis) {
|
||||
return new Date(millis);
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString, String pattern) throws ParseException {
|
||||
return new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sql;
|
||||
package com.baeldung.legacy.sqlpackage;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sql;
|
||||
package com.baeldung.legacy.sqlpackage;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.sql;
|
||||
package com.baeldung.legacy.sqlpackage;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -11,7 +11,7 @@ public class TimestampUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentTimestamp_thenNowIsReturned() {
|
||||
assertEquals(TimestampUtils.getNow().toInstant(), new Date().toInstant());
|
||||
assertEquals(TimestampUtils.getNow().getTime(), new Date().getTime());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.legacy.utilpackage;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class CalendarUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException {
|
||||
Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd");
|
||||
Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd");
|
||||
assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.legacy.utilpackage;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTimeMillis_thenDateIsReturned() {
|
||||
Date now = DateUtils.getNow();
|
||||
assertEquals(DateUtils.getDate(now.getTime()), now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
|
||||
long milliseconds = new Date(2020 - 1900, 0, 1).getTime();
|
||||
assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user