diff --git a/README.md b/README.md index 35d24c8..cf560ef 100644 --- a/README.md +++ b/README.md @@ -1,166 +1,184 @@ -## Date Utils ## - -A collection of useful date utilities. - -Date string to Date - -```java -Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); -``` - -Date to string - -```java -String dateString = DateUtils.toString(date, "yyyy-MM-dd"); -``` - -Get year from Date - -```java -int year = DateUtils.getYear(date); -``` - -Get month from Date - -```java -int month = DateUtils.getMonth(date); -``` - -Get day from Date - -```java -int day = DateUtils.getDay(date); -``` - -Get hour from Date - -```java -int hour = DateUtils.getHour(date); -``` - -Get minute from Date - -```java -int minute = DateUtils.getMinute(date); -``` - -Get second from Date - -```java -int second = DateUtils.getSecond(date); -``` - -Find if Date is AM - -```java -boolean am = DateUtils.isAM(date); -``` - -Find if Date is PM - -```java -boolean pm = DateUtils.isPM(date); -``` - -Get day of the week string from Date (e.g. "Wednesday") - -```java -String day = DateUtils.getDayOfWeekString(date); -``` - -Add year(s) to Date - -```java -Date newDate = DateUtils.addYears(date, 2); -``` - - -Add month(s) to Date - -```java -Date newDate = DateUtils.addMonths(date, 2); -``` - -Add week(s) to Date - -```java -Date newDate = DateUtils.addWeeks(date, 2); -``` - -Add day(s) to Date - -```java -Date newDate = DateUtils.addDays(date, 2); -``` - -Add hour(s) to Date - -```java -Date newDate = DateUtils.addHours(date, 2); -``` - -Add minute(s) to Date - -```java -Date newDate = DateUtils.addMinutes(date, 2); -``` - -Add second(s) to Date - -```java -Date newDate = DateUtils.addSeconds(date, 45); -``` - -Truncate Date - -```java -Date newDate = DateUtils.truncate(date); -``` - -Get number of years between two Dates - -```java -long years = DateUtils.yearsBetween(startDate, endDate); -``` - -Get number of months between two Dates - -```java -long months = DateUtils.monthsBetween(startDate, endDate); -``` - -Get number of weeks between two Dates - -```java -long weeks = DateUtils.weeksBetween(startDate, endDate); -``` - -Get number of days between two Dates - -```java -long days = DateUtils.daysBetween(startDate, endDate); -``` - -Get number of hours between two Dates - -```java -long hours = DateUtils.hoursBetween(startDate, endDate); -``` - -Get number of minutes between two Dates - -```java -long minutes = DateUtils.minutesBetween(startDate, endDate); -``` - -Get number of seconds between two Dates - -```java -long seconds = DateUtils.secondsBetween(startDate, endDate); -``` - -Find if Date is a Leap Year - -```java -boolean leapYear = DateUtils.isLeapYear(date); -``` +## Date Utils ## + +A collection of useful date utilities. + +Date string to Date + +```java +Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); +``` + +Date to string + +```java +String dateString = DateUtils.toString(date, "yyyy-MM-dd"); +``` + +Get year from Date + +```java +int year = DateUtils.getYear(date); +``` + +Get month from Date + +```java +int month = DateUtils.getMonth(date); +``` + +Get day from Date + +```java +int day = DateUtils.getDay(date); +``` + +Get hour from Date + +```java +int hour = DateUtils.getHour(date); +``` + +Get minute from Date + +```java +int minute = DateUtils.getMinute(date); +``` + +Get second from Date + +```java +int second = DateUtils.getSecond(date); +``` + +Find if Date is AM + +```java +boolean am = DateUtils.isAM(date); +``` + +Find if Date is PM + +```java +boolean pm = DateUtils.isPM(date); +``` + +Check if is weekend day + +```java +boolean isWeekend = DateUtils.isWeekend(date); +``` + +Check if is business day (M-F) + +```java +boolean isBusinessDay = DateUtils.isBusinessDay(date); +``` + +Find day of the week number (Monday = 1 ... Sunday = 7) + +```java +int number = DateUtils.getDayOfWeekNumber(date); +``` + +Get day of the week string from Date (e.g. "Wednesday") + +```java +String day = DateUtils.getDayOfWeekString(date); +``` + +Add year(s) to Date + +```java +Date newDate = DateUtils.addYears(date, 2); +``` + + +Add month(s) to Date + +```java +Date newDate = DateUtils.addMonths(date, 2); +``` + +Add week(s) to Date + +```java +Date newDate = DateUtils.addWeeks(date, 2); +``` + +Add day(s) to Date + +```java +Date newDate = DateUtils.addDays(date, 2); +``` + +Add hour(s) to Date + +```java +Date newDate = DateUtils.addHours(date, 2); +``` + +Add minute(s) to Date + +```java +Date newDate = DateUtils.addMinutes(date, 2); +``` + +Add second(s) to Date + +```java +Date newDate = DateUtils.addSeconds(date, 45); +``` + +Truncate Date + +```java +Date newDate = DateUtils.truncate(date); +``` + +Get number of years between two Dates + +```java +long years = DateUtils.yearsBetween(startDate, endDate); +``` + +Get number of months between two Dates + +```java +long months = DateUtils.monthsBetween(startDate, endDate); +``` + +Get number of weeks between two Dates + +```java +long weeks = DateUtils.weeksBetween(startDate, endDate); +``` + +Get number of days between two Dates + +```java +long days = DateUtils.daysBetween(startDate, endDate); +``` + +Get number of hours between two Dates + +```java +long hours = DateUtils.hoursBetween(startDate, endDate); +``` + +Get number of minutes between two Dates + +```java +long minutes = DateUtils.minutesBetween(startDate, endDate); +``` + +Get number of seconds between two Dates + +```java +long seconds = DateUtils.secondsBetween(startDate, endDate); +``` + +Find if Date is a Leap Year + +```java +boolean leapYear = DateUtils.isLeapYear(date); +``` diff --git a/pom.xml b/pom.xml index 0d32c8f..5bd27eb 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ org.projectlombok lombok - 1.18.22 + 1.18.38 provided diff --git a/src/main/java/io/ljunggren/date/utils/DateUtils.java b/src/main/java/io/ljunggren/date/utils/DateUtils.java index f32ba60..4f5796c 100644 --- a/src/main/java/io/ljunggren/date/utils/DateUtils.java +++ b/src/main/java/io/ljunggren/date/utils/DateUtils.java @@ -1,130 +1,143 @@ -package io.ljunggren.date.utils; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.Year; -import java.util.Date; - -import org.joda.time.Days; -import org.joda.time.Hours; -import org.joda.time.LocalDate; -import org.joda.time.LocalDateTime; -import org.joda.time.Minutes; -import org.joda.time.Months; -import org.joda.time.Seconds; -import org.joda.time.Weeks; -import org.joda.time.Years; - -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 LocalDateTime(date).getYear(); - } - - public static int getMonth(Date date) { - return new LocalDateTime(date).getMonthOfYear(); - } - - public static int getDay(Date date) { - return new LocalDateTime(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 boolean isAM(Date date) { - return getHour(date) < 12; - } - - public static boolean isPM(Date date) { - return !isAM(date); - } - - public static String getDayOfWeekString(Date date) { - return new LocalDate(date).dayOfWeek().getAsText(); - } - - public static Date addYears(Date date, int years) { - return new LocalDateTime(date).plusYears(years).toDate(); - } - - public static Date addMonths(Date date, int months) { - return new LocalDateTime(date).plusMonths(months).toDate(); - } - - public static Date addWeeks(Date date, int weeks) { - return new LocalDateTime(date).plusWeeks(weeks).toDate(); - } - - public static Date addDays(Date date, int days) { - return new LocalDateTime(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(); - } - - public static Date truncate(Date date) { - return new LocalDate(date).toDate(); - } - - public static long yearsBetween(Date startDate, Date endDate) { - return Years.yearsBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getYears(); - } - - public static long monthsBetween(Date startDate, Date endDate) { - return Months.monthsBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getMonths(); - } - - public static long weeksBetween(Date startDate, Date endDate) { - return Weeks.weeksBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getWeeks(); - } - - public static long daysBetween(Date startDate, Date endDate) { - return Days.daysBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getDays(); - } - - public static long hoursBetween(Date startDate, Date endDate) { - return Hours.hoursBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getHours(); - } - - public static long minutesBetween(Date startDate, Date endDate) { - return Minutes.minutesBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getMinutes(); - } - - public static long secondsBetween(Date startDate, Date endDate) { - return Seconds.secondsBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getSeconds(); - } - - public static boolean isLeapYear(Date date) { - return Year.isLeap(getYear(date)); - } - -} +package io.ljunggren.date.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.Year; +import java.util.Date; + +import org.joda.time.Days; +import org.joda.time.Hours; +import org.joda.time.LocalDate; +import org.joda.time.LocalDateTime; +import org.joda.time.Minutes; +import org.joda.time.Months; +import org.joda.time.Seconds; +import org.joda.time.Weeks; +import org.joda.time.Years; + +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 LocalDateTime(date).getYear(); + } + + public static int getMonth(Date date) { + return new LocalDateTime(date).getMonthOfYear(); + } + + public static int getDay(Date date) { + return new LocalDateTime(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 boolean isAM(Date date) { + return getHour(date) < 12; + } + + public static boolean isPM(Date date) { + return !isAM(date); + } + + public static boolean isWeekend(Date date) { + int dayOfWeek = getDayOfWeekNumber(date); + return dayOfWeek == 6 || dayOfWeek == 7; + } + + public static boolean isBusinessDay(Date date) { + return !isWeekend(date); + } + + public static int getDayOfWeekNumber(Date date) { + return new LocalDate(date).getDayOfWeek(); + } + + public static String getDayOfWeekString(Date date) { + return new LocalDate(date).dayOfWeek().getAsText(); + } + + public static Date addYears(Date date, int years) { + return new LocalDateTime(date).plusYears(years).toDate(); + } + + public static Date addMonths(Date date, int months) { + return new LocalDateTime(date).plusMonths(months).toDate(); + } + + public static Date addWeeks(Date date, int weeks) { + return new LocalDateTime(date).plusWeeks(weeks).toDate(); + } + + public static Date addDays(Date date, int days) { + return new LocalDateTime(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(); + } + + public static Date truncate(Date date) { + return new LocalDate(date).toDate(); + } + + public static long yearsBetween(Date startDate, Date endDate) { + return Years.yearsBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getYears(); + } + + public static long monthsBetween(Date startDate, Date endDate) { + return Months.monthsBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getMonths(); + } + + public static long weeksBetween(Date startDate, Date endDate) { + return Weeks.weeksBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getWeeks(); + } + + public static long daysBetween(Date startDate, Date endDate) { + return Days.daysBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getDays(); + } + + public static long hoursBetween(Date startDate, Date endDate) { + return Hours.hoursBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getHours(); + } + + public static long minutesBetween(Date startDate, Date endDate) { + return Minutes.minutesBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getMinutes(); + } + + public static long secondsBetween(Date startDate, Date endDate) { + return Seconds.secondsBetween(new LocalDateTime(startDate), new LocalDateTime(endDate)).getSeconds(); + } + + public static boolean isLeapYear(Date date) { + return Year.isLeap(getYear(date)); + } + +} diff --git a/src/test/java/io/ljunggren/date/utils/DateUtilsTest.java b/src/test/java/io/ljunggren/date/utils/DateUtilsTest.java index 60da459..b1bbf9d 100644 --- a/src/test/java/io/ljunggren/date/utils/DateUtilsTest.java +++ b/src/test/java/io/ljunggren/date/utils/DateUtilsTest.java @@ -1,227 +1,255 @@ -package io.ljunggren.date.utils; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -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 day = DateUtils.getDay(date); - assertEquals(1, day); - } - - @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 isAMTest() throws ParseException { - Date date = DateUtils.toDate("2021-12-01 01:45:30", "yyyy-MM-dd HH:mm:ss"); - boolean am = DateUtils.isAM(date); - assertTrue(am); - } - - @Test - public void isPMTest() throws ParseException { - Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - boolean pm = DateUtils.isPM(date); - assertTrue(pm); - } - - @Test - public void getDayOfWeekStringTest() throws ParseException { - Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); - String day = DateUtils.getDayOfWeekString(date); - assertEquals("Wednesday", day); - } - - @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)); - } - - @Test - public void truncateTest() throws ParseException { - Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date truncated = DateUtils.truncate(date); - assertEquals(0, DateUtils.getHour(truncated)); - assertEquals(0, DateUtils.getMinute(truncated)); - assertEquals(0, DateUtils.getSecond(truncated)); - } - - @Test - public void yearsBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2024-12-14 18:00:00", "yyyy-MM-dd HH:mm:ss"); - long years = DateUtils.yearsBetween(startDate, endDate); - assertEquals(3, years); - } - - @Test - public void monthsBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2022-02-14 18:00:00", "yyyy-MM-dd HH:mm:ss"); - long months = DateUtils.monthsBetween(startDate, endDate); - assertEquals(2, months); - } - - @Test - public void weeksBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2022-02-14 18:00:00", "yyyy-MM-dd HH:mm:ss"); - long weeks = DateUtils.weeksBetween(startDate, endDate); - assertEquals(10, weeks); - } - - @Test - public void daysBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2021-12-25 18:00:00", "yyyy-MM-dd HH:mm:ss"); - long days = DateUtils.daysBetween(startDate, endDate); - assertEquals(24, days); - } - - @Test - public void hoursBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2021-12-02 18:00:00", "yyyy-MM-dd HH:mm:ss"); - long hours = DateUtils.hoursBetween(startDate, endDate); - assertEquals(28, hours); - } - - @Test - public void minutesBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2021-12-01 15:00:00", "yyyy-MM-dd HH:mm:ss"); - long minutes = DateUtils.minutesBetween(startDate, endDate); - assertEquals(74, minutes); - } - - @Test - public void secondsBetweenTest() throws ParseException { - Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); - Date endDate = DateUtils.toDate("2021-12-01 13:47:00", "yyyy-MM-dd HH:mm:ss"); - long seconds = DateUtils.secondsBetween(startDate, endDate); - assertEquals(90, seconds); - } - - @Test - public void isLeapYearTest() throws ParseException { - Date date = DateUtils.toDate("2020-01-01", "yyyy-MM-dd"); - assertTrue(DateUtils.isLeapYear(date)); - } - - @Test - public void isLeapYearFalseTest() throws ParseException { - Date date = DateUtils.toDate("2021-01-01", "yyyy-MM-dd"); - assertFalse(DateUtils.isLeapYear(date)); - } - -} +package io.ljunggren.date.utils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +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 day = DateUtils.getDay(date); + assertEquals(1, day); + } + + @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 isAMTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 01:45:30", "yyyy-MM-dd HH:mm:ss"); + boolean am = DateUtils.isAM(date); + assertTrue(am); + } + + @Test + public void isPMTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + boolean pm = DateUtils.isPM(date); + assertTrue(pm); + } + + @Test + public void isWeekendTest() throws ParseException { + assertTrue(DateUtils.isWeekend(DateUtils.toDate("2025-09-07", "yyyy-MM-dd"))); // Sunday + assertFalse(DateUtils.isWeekend(DateUtils.toDate("2025-09-08", "yyyy-MM-dd"))); // Monday + assertFalse(DateUtils.isWeekend(DateUtils.toDate("2025-09-09", "yyyy-MM-dd"))); // Tuesday + assertFalse(DateUtils.isWeekend(DateUtils.toDate("2025-09-10", "yyyy-MM-dd"))); // Wednesday + assertFalse(DateUtils.isWeekend(DateUtils.toDate("2025-09-11", "yyyy-MM-dd"))); // Thursday + assertFalse(DateUtils.isWeekend(DateUtils.toDate("2025-09-12", "yyyy-MM-dd"))); // Friday + assertTrue(DateUtils.isWeekend(DateUtils.toDate("2025-09-13", "yyyy-MM-dd"))); // Saturday + } + + @Test + public void isBusinessDayTest() throws ParseException { + assertFalse(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-07", "yyyy-MM-dd"))); // Sunday + assertTrue(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-08", "yyyy-MM-dd"))); // Monday + assertTrue(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-09", "yyyy-MM-dd"))); // Tuesday + assertTrue(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-10", "yyyy-MM-dd"))); // Wednesday + assertTrue(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-11", "yyyy-MM-dd"))); // Thursday + assertTrue(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-12", "yyyy-MM-dd"))); // Friday + assertFalse(DateUtils.isBusinessDay(DateUtils.toDate("2025-09-13", "yyyy-MM-dd"))); // Saturday + } + + @Test + public void getDayOfWeekNumberTest() throws ParseException { + Date date = DateUtils.toDate("2025-09-20", "yyyy-MM-dd"); + assertEquals(6, DateUtils.getDayOfWeekNumber(date)); + } + + @Test + public void getDayOfWeekStringTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01", "yyyy-MM-dd"); + String day = DateUtils.getDayOfWeekString(date); + assertEquals("Wednesday", day); + } + + @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)); + } + + @Test + public void truncateTest() throws ParseException { + Date date = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date truncated = DateUtils.truncate(date); + assertEquals(0, DateUtils.getHour(truncated)); + assertEquals(0, DateUtils.getMinute(truncated)); + assertEquals(0, DateUtils.getSecond(truncated)); + } + + @Test + public void yearsBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2024-12-14 18:00:00", "yyyy-MM-dd HH:mm:ss"); + long years = DateUtils.yearsBetween(startDate, endDate); + assertEquals(3, years); + } + + @Test + public void monthsBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2022-02-14 18:00:00", "yyyy-MM-dd HH:mm:ss"); + long months = DateUtils.monthsBetween(startDate, endDate); + assertEquals(2, months); + } + + @Test + public void weeksBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2022-02-14 18:00:00", "yyyy-MM-dd HH:mm:ss"); + long weeks = DateUtils.weeksBetween(startDate, endDate); + assertEquals(10, weeks); + } + + @Test + public void daysBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2021-12-25 18:00:00", "yyyy-MM-dd HH:mm:ss"); + long days = DateUtils.daysBetween(startDate, endDate); + assertEquals(24, days); + } + + @Test + public void hoursBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2021-12-02 18:00:00", "yyyy-MM-dd HH:mm:ss"); + long hours = DateUtils.hoursBetween(startDate, endDate); + assertEquals(28, hours); + } + + @Test + public void minutesBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2021-12-01 15:00:00", "yyyy-MM-dd HH:mm:ss"); + long minutes = DateUtils.minutesBetween(startDate, endDate); + assertEquals(74, minutes); + } + + @Test + public void secondsBetweenTest() throws ParseException { + Date startDate = DateUtils.toDate("2021-12-01 13:45:30", "yyyy-MM-dd HH:mm:ss"); + Date endDate = DateUtils.toDate("2021-12-01 13:47:00", "yyyy-MM-dd HH:mm:ss"); + long seconds = DateUtils.secondsBetween(startDate, endDate); + assertEquals(90, seconds); + } + + @Test + public void isLeapYearTest() throws ParseException { + Date date = DateUtils.toDate("2020-01-01", "yyyy-MM-dd"); + assertTrue(DateUtils.isLeapYear(date)); + } + + @Test + public void isLeapYearFalseTest() throws ParseException { + Date date = DateUtils.toDate("2021-01-01", "yyyy-MM-dd"); + assertFalse(DateUtils.isLeapYear(date)); + } + +}