diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..85590f5 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## Date Utils ## + +A collection of usefule date utilities. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..63cd3f0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,38 @@ + + 4.0.0 + com.ljunggren + date-utils + jar + 0.0.1-SNAPSHOT + + date-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + joda-time + joda-time + 2.10.13 + + + org.projectlombok + lombok + 1.18.12 + provided + + + junit + junit + 4.13 + test + + + diff --git a/src/main/java/com/ljunggren/dateUtils/DateUtils.java b/src/main/java/com/ljunggren/dateUtils/DateUtils.java new file mode 100644 index 0000000..4db085b --- /dev/null +++ b/src/main/java/com/ljunggren/dateUtils/DateUtils.java @@ -0,0 +1,74 @@ +package com.ljunggren.dateUtils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.joda.time.LocalDate; +import org.joda.time.LocalDateTime; + +public class DateUtils { + + public static Date toDate(String dateString, String pattern) throws ParseException { + SimpleDateFormat formatter = new SimpleDateFormat(pattern); + return formatter.parse(dateString); + } + + public static String toString(Date date, String pattern) { + SimpleDateFormat formatter = new SimpleDateFormat(pattern); + return formatter.format(date); + } + + public static int getYear(Date date) { + return new LocalDate(date).getYear(); + } + + public static int getMonth(Date date) { + return new LocalDate(date).getMonthOfYear(); + } + + public static int getDay(Date date) { + return new LocalDate(date).getDayOfMonth(); + } + + public static int getHour(Date date) { + return new LocalDateTime(date).getHourOfDay(); + } + + public static int getMinute(Date date) { + return new LocalDateTime(date).getMinuteOfHour(); + } + + public static int getSecond(Date date) { + return new LocalDateTime(date).getSecondOfMinute(); + } + + public static Date addYears(Date date, int years) { + return new LocalDate(date).plusYears(years).toDate(); + } + + public static Date addMonths(Date date, int months) { + return new LocalDate(date).plusMonths(months).toDate(); + } + + public static Date addWeeks(Date date, int weeks) { + return new LocalDate(date).plusWeeks(weeks).toDate(); + } + + public static Date addDays(Date date, int days) { + return new LocalDate(date).plusDays(days).toDate(); + } + + public static Date addHours(Date date, int hours) { + return new LocalDateTime(date).plusHours(hours).toDate(); + } + + public static Date addMinutes(Date date, int minutes) { + return new LocalDateTime(date).plusMinutes(minutes).toDate(); + } + + public static Date addSeconds(Date date, int seconds) { + return new LocalDateTime(date).plusSeconds(seconds).toDate(); + } + +} diff --git a/src/test/java/com/ljunggren/dateUtils/DateUtilsTest.java b/src/test/java/com/ljunggren/dateUtils/DateUtilsTest.java new file mode 100644 index 0000000..f9f45d0 --- /dev/null +++ b/src/test/java/com/ljunggren/dateUtils/DateUtilsTest.java @@ -0,0 +1,127 @@ +package com.ljunggren.dateUtils; + +import static org.junit.Assert.*; + +import java.text.ParseException; +import java.util.Date; + +import org.junit.Test; + +public class DateUtilsTest { + + @Test + public void toDateTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + assertEquals(2021, DateUtils.getYear(date)); + assertEquals(12, DateUtils.getMonth(date)); + assertEquals(1, DateUtils.getDay(date)); + } + + @Test + public void toStringTest() throws ParseException { + String pattern = "yyyy-MM-dd"; + String dateString = "2021-12-01"; + Date date = DateUtils.toDate("2021-12-01", pattern); + String toString = DateUtils.toString(date, pattern); + assertEquals(dateString, toString); + } + + @Test + public void getYearTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + int year = DateUtils.getYear(date); + assertEquals(2021, year); + } + + @Test + public void getMonthTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + int month = DateUtils.getMonth(date); + assertEquals(12, month); + } + + @Test + public void getDayTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + int month = DateUtils.getDay(date); + assertEquals(1, month); + } + + @Test + public void getHourTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + int hour = DateUtils.getHour(date); + assertEquals(13, hour); + } + + @Test + public void getMinuteTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + int minute = DateUtils.getMinute(date); + assertEquals(45, minute); + } + + @Test + public void getSecondTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + int second = DateUtils.getSecond(date); + assertEquals(30, second); + } + + @Test + public void addYearsTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + Date updated = DateUtils.addYears(date, -2); + assertEquals(2019, DateUtils.getYear(updated)); + } + + @Test + public void addMonthsTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + Date updated = DateUtils.addMonths(date, 3); + assertEquals(3, DateUtils.getMonth(updated)); + assertEquals(2022, DateUtils.getYear(updated)); + } + + @Test + public void addWeeksTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + Date updated = DateUtils.addWeeks(date, 5); + assertEquals(5, DateUtils.getDay(updated)); + assertEquals(1, DateUtils.getMonth(updated)); + assertEquals(2022, DateUtils.getYear(updated)); + } + + @Test + public void addDaysTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + Date updated = DateUtils.addDays(date, 31); + assertEquals(1, DateUtils.getDay(updated)); + assertEquals(1, DateUtils.getMonth(updated)); + assertEquals(2022, DateUtils.getYear(updated)); + } + + @Test + public void addHoursTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date updated = DateUtils.addHours(date, 2); + assertEquals(15, DateUtils.getHour(updated)); + } + + @Test + public void addMinutesTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date updated = DateUtils.addMinutes(date, 15); + assertEquals(14, DateUtils.getHour(updated)); + assertEquals(0, DateUtils.getMinute(updated)); + } + + @Test + public void addSecondsTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date updated = DateUtils.addSeconds(date, 45); + assertEquals(46, DateUtils.getMinute(updated)); + assertEquals(15, DateUtils.getSecond(updated)); + } + +}