Package com.jk.core.util
Class JKDateTimeUtil
java.lang.Object
com.jk.core.util.JKDateTimeUtil
This class is a utility class used for handling date and time related
operations.
- Version:
- 1.0
- Author:
- Dr. Jalal H. Kiswani
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
This enum is representing the comparison result between two dates. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Date
adddDaysToCurrentDate
(int numberOfDays) This method adds a specified number of days to the current date and returns the resulting date.static Date
adddMonths
(int numOfMonths) This method adds a specified number of months to the current date and returns the resulting date.static Date
This method adds the specified number of months to the given date.static JKDateTimeUtil.CompareDates
compareTwoDates
(Date date1, Date date2) This method compares two dates and returns the relationship between them.static Date
createDate
(int day, int month, int year) This method creates aDate
object with the specified day, month, and year.static boolean
This method checks if two dates are equal in terms of year, month, and day.static String
This method formats the current date in the pattern"yyyy-MM-dd"
.static String
This method formats the current time stamp in the pattern"yyyy-MM-dd HH:mm:ss"
.static String
formatDate
(Date date, String pattren) This method formats a given Date object using the specified pattern.static String
formatFullTime
(Date date) This method formats the time portion of a given Date object in the"hh:mm:ss SSS"
format.static String
formatTime
(Date date) This method formats the given date as a string representing the time (hour and minute).static int
This method gets the current day of the month.static int
This method gets the current month.static String
This method gets the current time in the"hh:mm:ss SSS"
format.static String
This method gets the current time stamp in the"yyyy/MM/dd hh:mm:ss"
format.static int
This method gets the current year.static JKTimeObject
This method retrieves the current time as aJKTimeObject
.static long
getDayDifference
(Date startDate, Date endDate) This method calculates the difference in days between two dates.static int
getDayOfWeek
(Date date) This method gets the day of the week from a given date.static long
getDifference
(Time timeFrom, Time timeTo) This method calculates the difference in minutes between two Time objects.static int
This method retrieves the hour component of a given time.static long
getHoursDifference
(Date startDate, Date endDate) This method calculates the difference in hours between two dates.static long
getMillisDifference
(Date startDate, Date endDate) This method calculates the difference in milliseconds between two dates.static long
getMinutesDifference
(Date startDate, Date endDate) This method calculates the difference in minutes between two dates.static int
getMonthFromDate
(Date date) This method gets the month from a given date.static int
getNumOfMonths
(Date date1, Date date2) This method calculates the number of months between two dates.static long
getSecondsDifference
(Date startDate, Date endDate) This method calculates the difference in seconds between two dates.static Date
This method is yet to be implemented correctly.static int
getYearFromData
(Date date) This method gets the year from a given date.static boolean
isCurrentTimeBetweenTowTimes
(Date fromDate, Date fromTime, Date toDate, Date timeTo) This method checks if the current time is between two specified time ranges.static boolean
This method checks if the given string can be parsed as a valid date using the specified pattern.static boolean
isDateEqaualed
(Date date1, Date date2) This method compares two Date objects to determine if they represent the same date (year, month, and day) regardless of their time components.static boolean
isPeriodActive
(Date startDate, Date endDate) This method checks if a given period defined by start and end dates is currently active.static boolean
isTimeConflict
(Time timeFrom, Time timeTo, Time otherTimeFrom, Time otherTimeTo) This method checks if there is a time conflict between two time intervals.static boolean
isTimesEqaualed
(Date time1, Date time2) This method checks if the two provided dates are equal or not.static void
This main method demonstrates the usage of this class by creating two Date objects then printing them, as well as printing the difference in hours between them.static Date
This method parses a date string using the default date pattern.static Date
This method parses a string representation of a date using the specified pattern.
-
Constructor Details
-
JKDateTimeUtil
public JKDateTimeUtil()
-
-
Method Details
-
parseDate
This method parses a string representation of a date using the specified pattern.- Parameters:
strDate
- Specifies the string representation of the date.pattern
- Specifies the pattern to use for parsing the date.- Returns:
- a date object representing the parsed date.
-
formatDate
This method formats a given Date object using the specified pattern.- Parameters:
date
- Specifies the Date object to be formatted.pattren
- Specifies the pattern to use for formatting the date.- Returns:
- a formatted string representing the date.
-
formatFullTime
This method formats the time portion of a given Date object in the"hh:mm:ss SSS"
format.- Parameters:
date
- Specifies the Date object to be formatted.- Returns:
- the formatted string representing the time in "hh:mm:ss SSS" format.
-
getCurrentTime
This method gets the current time in the"hh:mm:ss SSS"
format.- Returns:
- the formatted string representing the current time in "hh:mm:ss SSS" format.
-
getCurrentTimeStamp
This method gets the current time stamp in the"yyyy/MM/dd hh:mm:ss"
format.- Returns:
- the formatted string representing the current time stamp in "yyyy/MM/dd hh:mm:ss" format.
-
getCurrentYear
public static int getCurrentYear()This method gets the current year.- Returns:
- the current year as an integer value.
-
getCurrentMonth
public static int getCurrentMonth()This method gets the current month.- Returns:
- the current month as an integer value (1 to 12).
-
getYearFromData
This method gets the year from a given date.- Parameters:
date
- Specifies the date from which to extract the year.- Returns:
- the year extracted from the given date.
-
getMonthFromDate
This method gets the month from a given date.- Parameters:
date
- Specifies the date from which to extract the month.- Returns:
- the month extracted from the given date.
-
formatTime
This method formats the given date as a string representing the time (hour and minute).- Parameters:
date
- Specifies the date to be formatted.- Returns:
- the string representing the time (hour and minute) of the given date.
-
isTimesEqaualed
This method checks if the two provided dates are equal or not.- Parameters:
time1
- Represents the first date to be compared.time2
- Represents the second date to be compared.- Returns:
- true, if both dates are equal, false otherwise.
-
getNumOfMonths
This method calculates the number of months between two dates.- Parameters:
date1
- Specifies the first date.date2
- Specifies the second date.- Returns:
- the number of months between the two dates.
-
formatCurrentDate
This method formats the current date in the pattern"yyyy-MM-dd"
.- Returns:
- the formatted current date.
-
formatCurrentTimeStamp
This method formats the current time stamp in the pattern"yyyy-MM-dd HH:mm:ss"
.- Returns:
- the formatted current time stamp.
-
compareTwoDates
This method compares two dates and returns the relationship between them.- Parameters:
date1
- Specifies the first date to compare.date2
- Specifies the second date to compare.- Returns:
- the value from the CompareDates enum indicating the relationship between the dates.
- See Also:
-
adddDaysToCurrentDate
This method adds a specified number of days to the current date and returns the resulting date.- Parameters:
numberOfDays
- Specifies the number of days to add to the current date.- Returns:
- the resulting date after adding the specified number of days to the current date.
-
isDate
This method checks if the given string can be parsed as a valid date using the specified pattern.- Parameters:
strDate
- Specifies the string representing the date to be checked.pattern
- Specifies the pattern used to parse the date.- Returns:
- true, if the string can be parsed as a valid date, false otherwise.
-
adddMonths
This method adds a specified number of months to the current date and returns the resulting date.- Parameters:
numOfMonths
- Specifies the number of months to add to the current date.- Returns:
- the resulting date after adding the specified number of months to the current date.
-
addMonths
This method adds the specified number of months to the given date.- Parameters:
date
- Specifies the date to which months will be added.numOfMonths
- Specifies the number of months to add.- Returns:
- a new Date object representing the result of adding the specified number of months to the given date.
-
getDifference
This method calculates the difference in minutes between two Time objects.- Parameters:
timeFrom
- Specifies the starting time.timeTo
- Specifies the ending time.- Returns:
- the difference in minutes between the two Time objects.
-
getCurrentDayInMonth
public static int getCurrentDayInMonth()This method gets the current day of the month.- Returns:
- the current day of the month.
-
getDayOfWeek
This method gets the day of the week from a given date.- Parameters:
date
- Specifies the date from which to extract the day of the week.- Returns:
- the day of the week.
-
isTimeConflict
public static boolean isTimeConflict(Time timeFrom, Time timeTo, Time otherTimeFrom, Time otherTimeTo) This method checks if there is a time conflict between two time intervals.- Parameters:
timeFrom
- Specifies the start time of the first interval.timeTo
- Specifies the end time of the first interval.otherTimeFrom
- Specifies the start time of the second interval.otherTimeTo
- Specifies the end time of the second interval.- Returns:
- true, if there is a time conflict, otherwise false.
-
getHour
This method retrieves the hour component of a given time.- Parameters:
timeFrom
- Specifies the time from which to extract the hour.- Returns:
- the hour component of the provided time.
-
equals
This method checks if two dates are equal in terms of year, month, and day.- Parameters:
date1
- Specifies the first date to compare.date2
- Specifies the second date to compare.- Returns:
- true, the dates have the same year, month, and day, otherwise, false.
-
isCurrentTimeBetweenTowTimes
public static boolean isCurrentTimeBetweenTowTimes(Date fromDate, Date fromTime, Date toDate, Date timeTo) This method checks if the current time is between two specified time ranges.- Parameters:
fromDate
- Specifies the date part of the starting time range.fromTime
- Specifies the time part of the starting time range.toDate
- Specifies the date part of the ending time range.timeTo
- Specifies the time part of the ending time range.- Returns:
- true, if the current time is between the specified time ranges, false otherwise.
-
getCurrntTime
This method retrieves the current time as aJKTimeObject
.- Returns:
- a
JKTimeObject
representing the current time.
-
getDayDifference
This method calculates the difference in days between two dates.- Parameters:
startDate
- Specifies the start date.endDate
- Specifies the end date.- Returns:
- the difference in days between the start and end dates.
-
getSecondsDifference
This method calculates the difference in seconds between two dates.- Parameters:
startDate
- Specifies the start date.endDate
- Specifies the end date.- Returns:
- the difference in seconds between the start and end dates.
-
getHoursDifference
This method calculates the difference in hours between two dates.- Parameters:
startDate
- Specifies the start date.endDate
- Specifies the end date.- Returns:
- the difference in hours between the start and end dates.
-
getMillisDifference
This method calculates the difference in milliseconds between two dates.- Parameters:
startDate
- Specifies the start date.endDate
- Specifies the end date.- Returns:
- the difference in milliseconds between the start and end dates.
-
getMinutesDifference
This method calculates the difference in minutes between two dates.- Parameters:
startDate
- Specifies the start date.endDate
- Specifies the end date.- Returns:
- the difference in minutes between the start and end dates.
-
main
This main method demonstrates the usage of this class by creating two Date objects then printing them, as well as printing the difference in hours between them.- Parameters:
args
- the command-line arguments.
-
isDateEqaualed
This method compares two Date objects to determine if they represent the same date (year, month, and day) regardless of their time components.- Parameters:
date1
- Specifies the first Date to comparedate2
- Specifies the second Date to compare- Returns:
- true, if the dates represent the same day, false otherwise.
-
isPeriodActive
This method checks if a given period defined by start and end dates is currently active.- Parameters:
startDate
- Specifies the start date of the period.endDate
- Specifies the end date of the period.- Returns:
- true, if the period is currently active, false otherwise.
-
getSystemDate
This method is yet to be implemented correctly. (TBI).- Returns:
- the current date. But this is bound to change when the method gets implemented. (TBI)
-
parseDate
This method parses a date string using the default date pattern.- Parameters:
date
- Specifies the date string to be parsed.- Returns:
- the parsed
Date
object.
-
createDate
This method creates aDate
object with the specified day, month, and year.- Parameters:
day
- Specifies the day of the month.month
- Specifies the month (1-12).year
- Specifies the year.- Returns:
- the created
Date
object.
-