Skip to content

Commit f476af1

Browse files
authored
add datetime test case (#17865)
1 parent cfda139 commit f476af1

File tree

10 files changed

+192
-0
lines changed

10 files changed

+192
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.datetime;
2+
3+
import java.text.ParseException;
4+
import java.util.Calendar;
5+
import java.util.Date;
6+
7+
public class CalendarUtils {
8+
public static Calendar getPlusDays(Date date, int amount) throws ParseException {
9+
Calendar calendar = Calendar.getInstance();
10+
calendar.setTime(date);
11+
calendar.add(Calendar.DAY_OF_YEAR, amount);
12+
return calendar;
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.datetime;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.text.ParseException;
5+
import java.util.Date;
6+
public class DateUtils {
7+
8+
public static Date getNow() {
9+
return new Date();
10+
}
11+
12+
public static Date getDate(long millis) {
13+
return new Date(millis);
14+
}
15+
16+
public static Date getDate(String dateAsString, String pattern) throws ParseException {
17+
return new SimpleDateFormat(pattern).parse(dateAsString);
18+
}
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.datetime.sql;
2+
3+
import java.sql.Date;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
7+
public class DateUtils {
8+
9+
public static Date getNow() {
10+
return new Date(System.currentTimeMillis());
11+
}
12+
13+
public static Date getDate(String dateAsString) {
14+
return Date.valueOf(dateAsString);
15+
}
16+
17+
public static Date getDate(String dateAsString, String pattern) throws ParseException {
18+
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
19+
return new Date(customUtilDate.getTime());
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.datetime.sql;
2+
3+
import java.sql.Time;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
7+
public class TimeUtils {
8+
9+
public static Time getNow() {
10+
return new Time(System.currentTimeMillis());
11+
}
12+
13+
public static Time getTime(String timeAsString) {
14+
return Time.valueOf(timeAsString);
15+
}
16+
17+
public static Time getTime(String dateAsString, String pattern) throws ParseException {
18+
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
19+
return new Time(customUtilDate.getTime());
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.datetime.sql;
2+
3+
import java.sql.Timestamp;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
7+
public class TimestampUtils {
8+
9+
public static Timestamp getNow() {
10+
return new Timestamp(System.currentTimeMillis());
11+
}
12+
13+
public static Timestamp getTimestamp(String timestampAsString) {
14+
return Timestamp.valueOf(timestampAsString);
15+
}
16+
17+
public static Timestamp getTimestamp(String dateAsString, String pattern) throws ParseException {
18+
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
19+
return new Timestamp(customUtilDate.getTime());
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.datetime;
2+
3+
import org.junit.Test;
4+
5+
import java.text.ParseException;
6+
import java.util.Date;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class CalendarUtilsUnitTest {
11+
12+
@Test
13+
public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException {
14+
Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd");
15+
Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd");
16+
assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime());
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.datetime;
2+
import org.junit.Test;
3+
4+
import java.text.ParseException;
5+
import java.util.Date;
6+
7+
import static org.junit.Assert.assertEquals;
8+
public class DateUtilsUnitTest {
9+
10+
@Test
11+
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
12+
long milliseconds = new Date(2020 - 1900, 0, 1).getTime();
13+
assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
14+
}
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.datetime.sql;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.text.ParseException;
6+
import java.time.LocalDate;
7+
8+
import org.junit.Test;
9+
10+
public class DateUtilsUnitTest {
11+
12+
@Test(expected = IllegalArgumentException.class)
13+
public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
14+
DateUtils.getDate("2020 01 01");
15+
}
16+
17+
@Test
18+
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
19+
assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.datetime.sql;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.datetime.sql.TimeUtils;
8+
9+
import java.text.ParseException;
10+
11+
public class TimeUtilsUnitTest {
12+
13+
@Test(expected = IllegalArgumentException.class)
14+
public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
15+
TimeUtils.getTime("10 11 12");
16+
}
17+
18+
@Test
19+
public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException {
20+
assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss"));
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.datetime.sql;
2+
3+
import org.junit.Test;
4+
5+
import java.text.ParseException;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class TimestampUtilsUnitTest {
10+
11+
@Test(expected = IllegalArgumentException.class)
12+
public void givenTimestampAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
13+
TimestampUtils.getTimestamp("2020/01/01 10:11-12");
14+
}
15+
16+
@Test
17+
public void givenTimestampAndPattern_thenTimestampIsCorrectlyReturned() throws ParseException {
18+
assertEquals(TimestampUtils.getTimestamp("2020-01-01 10:11:12"), TimestampUtils.getTimestamp("2020/01/01 10:11-12", "yyyy/MM/dd hh:mm-ss"));
19+
}
20+
}

0 commit comments

Comments
 (0)