BAEL-3375 Working with Dates before Java 8
This commit is contained in:
17
core-java-modules/core-java-date-operations/pom.xml
Normal file
17
core-java-modules/core-java-date-operations/pom.xml
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-date-operations</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.sql;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
public static Date getNow() {
|
||||
return new Date(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString) {
|
||||
return Date.valueOf(dateAsString);
|
||||
}
|
||||
|
||||
public static Date getDate(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Date(customUtilDate.getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.sql;
|
||||
|
||||
import java.sql.Time;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class TimeUtils {
|
||||
|
||||
public static Time getNow() {
|
||||
return new Time(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Time getTime(String timeAsString) {
|
||||
return Time.valueOf(timeAsString);
|
||||
}
|
||||
|
||||
public static Time getTime(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Time(customUtilDate.getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.sql;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class TimestampUtils {
|
||||
|
||||
public static Timestamp getNow() {
|
||||
return new Timestamp(System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public static Timestamp getTimestamp(String timestampAsString) {
|
||||
return Timestamp.valueOf(timestampAsString);
|
||||
}
|
||||
|
||||
public static Timestamp getTimestamp(String dateAsString, String pattern) throws ParseException {
|
||||
java.util.Date customUtilDate = new SimpleDateFormat(pattern).parse(dateAsString);
|
||||
return new Timestamp(customUtilDate.getTime());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentDate_thenTodayIsReturned() {
|
||||
assertEquals(DateUtils.getNow(), new Date());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenDateAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
DateUtils.getDate("2020 01 01");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateAndPattern_thenDateIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(DateUtils.getDate("2020-01-01"), DateUtils.getDate("2020/01/01", "yyyy/MM/dd"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentTime_thenNowIsReturned() {
|
||||
assertEquals(TimeUtils.getNow(), new Date());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenTimeAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
TimeUtils.getTime("10 11 12");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimeAndPattern_thenTimeIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(TimeUtils.getTime("10:11:12"), TimeUtils.getTime("10 11 12", "hh mm ss"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.sql;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimestampUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCurrentTimestamp_thenNowIsReturned() {
|
||||
assertEquals(TimestampUtils.getNow().toInstant(), new Date().toInstant());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenTimestampAsString_whenPatternIsNotRespected_thenExceptionIsThrown() {
|
||||
TimestampUtils.getTimestamp("2020/01/01 10:11-12");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTimestampAndPattern_thenTimestampIsCorrectlyReturned() throws ParseException {
|
||||
assertEquals(TimestampUtils.getTimestamp("2020-01-01 10:11:12"), TimestampUtils.getTimestamp("2020/01/01 10:11-12", "yyyy/MM/dd hh:mm-ss"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user