Skip to content

Commit bb2f6b3

Browse files
authored
Merge pull request eugenp#8141 from JonCook/master
BAEL-3375 - Get the current Date in Java (non Java 8)
2 parents 06bea40 + 87b03b5 commit bb2f6b3

File tree

12 files changed

+247
-0
lines changed

12 files changed

+247
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-date-operations</artifactId>
7+
<version>${project.parent.version}</version>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-java</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../../parent-java</relativePath>
15+
</parent>
16+
17+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
9+
public static Calendar getPlusDays(Date date, int amount) throws ParseException {
10+
Calendar calendar = Calendar.getInstance();
11+
calendar.setTime(date);
12+
calendar.add(Calendar.DAY_OF_YEAR, amount);
13+
return calendar;
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.datetime;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Date;
6+
7+
public class DateUtils {
8+
9+
public static Date getNow() {
10+
return new Date();
11+
}
12+
13+
public static Date getDate(long millis) {
14+
return new Date(millis);
15+
}
16+
17+
public static Date getDate(String dateAsString, String pattern) throws ParseException {
18+
return new SimpleDateFormat(pattern).parse(dateAsString);
19+
}
20+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.datetime.CalendarUtils;
8+
import com.baeldung.datetime.DateUtils;
9+
10+
import java.text.ParseException;
11+
import java.util.Date;
12+
13+
public class CalendarUtilsUnitTest {
14+
15+
@Test
16+
public void givenDateAndDaysToAdd_thenCalendarIsCorrectlyReturned() throws ParseException {
17+
Date initialDate = DateUtils.getDate("2020/01/01", "yyyy/MM/dd");
18+
Date expectedDate= DateUtils.getDate("2020/01/11", "yyyy/MM/dd");
19+
assertEquals(expectedDate, CalendarUtils.getPlusDays(initialDate, 10).getTime());
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.datetime;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
import com.baeldung.datetime.DateUtils;
8+
9+
import java.text.ParseException;
10+
import java.util.Date;
11+
12+
public class DateUtilsUnitTest {
13+
14+
@Test
15+
public void givenTimeMillis_thenDateIsReturned() {
16+
Date now = DateUtils.getNow();
17+
assertEquals(DateUtils.getDate(now.getTime()), now);
18+
}
19+
20+
@Test
21+
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
22+
long milliseconds = new Date(2020 - 1900, 0, 1).getTime();
23+
assertEquals(DateUtils.getDate(milliseconds), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.DateUtils;
8+
9+
import java.text.ParseException;
10+
import java.util.Date;
11+
12+
public class DateUtilsUnitTest {
13+
14+
@Test
15+
public void givenCurrentDate_thenTodayIsReturned() {
16+
assertEquals(DateUtils.getNow(), new Date());
17+
}
18+
19+
@Test(expected = IllegalArgumentException.class)
20+
public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
21+
DateUtils.getDate("2020 01 01");
22+
}
23+
24+
@Test
25+
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
26+
assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
import java.util.Date;
11+
12+
public class TimeUtilsUnitTest {
13+
14+
@Test
15+
public void givenCurrentTime_thenNowIsReturned() {
16+
assertEquals(TimeUtils.getNow(), new Date());
17+
}
18+
19+
@Test(expected = IllegalArgumentException.class)
20+
public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
21+
TimeUtils.getTime("10 11 12");
22+
}
23+
24+
@Test
25+
public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException {
26+
assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss"));
27+
}
28+
}

0 commit comments

Comments
 (0)