
io.lsn.spring.utilities.helper.DateHelper Maven / Gradle / Ivy
package io.lsn.spring.utilities.helper;
import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.Years;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author Patryk Szlagowski
*/
public class DateHelper {
/**
* Converts Tibco's string-as-date to Java Date format
*
* @param tibcoStringDate
* @return
*/
public static Date tibcoStringToDate(String tibcoStringDate) {
return stringToDate(tibcoStringDate, "yyyy-MM-dd");
}
/**
* map string date to Date object with given pattern
* @param date
* @param pattern
* @return
*/
public static Date stringToDate(String date, String pattern) {
if ("0000-00-00".equals(date)) {
return null;
}
try {
DateFormat f = new SimpleDateFormat(pattern);
Date result = f.parse(date);
return result;
} catch (Exception e) {
return null;
}
}
/**
* converts date to localdate
*
* @param input
* @return
*/
public static LocalDate toLocalDate(Date input) {
if (input == null) {
return null;
}
LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return date;
}
/**
* return truncated date with alice formatting
*
* @param date
* @return
*/
public static String aliceShortDate(Date date) {
if (date != null) {
Date resultDate = truncate(date);
DateFormat f = new SimpleDateFormat("yyyy-MM-dd");
return f.format(resultDate);
} else {
return null;
}
}
/**
* return timestamp value of provided date
*
* @param date date for timestamp
* @return timestamp version of date
*/
public static String timestamp(Date date) {
if (date != null) {
DateFormat f = new SimpleDateFormat("yyyy-MM-dd H:mm:s:S");
return f.format(date);
} else {
return null;
}
}
/**
* current timestamp
*
* @return current timestamp
*/
public static String timestamp() {
return timestamp(new Date());
}
/**
* Calculate months between dates
*
* @param startDate
* @param endDate
* @return
*/
public static long ageInMonth(Date startDate, Date endDate) {
return Months.monthsBetween(new DateTime(startDate), new DateTime(endDate)).getMonths();
}
/**
* Calculate full years between dates
*
* @param startDate
* @param endDate
* @return
*/
public static long ageInYears(Date startDate, Date endDate) {
long ageInMonths = ageInMonth(startDate, endDate);
return Math.floorDiv(ageInMonths, 12);
}
/**
* Calculate full years since date
*
* @param startDate
* @return
*/
public static long ageInYears(Date startDate) {
long ageInMonths = ageInMonth(startDate);
return Math.floorDiv(ageInMonths, 12);
}
/**
* Calculate months between provided date and current timestamp
*
* @param startDate
* @return
*/
public static long ageInMonth(Date startDate) {
return ageInMonth(startDate, new Date());
}
/**
* Calculate days between dates
*
* @param startDate
* @param endDate
* @return
*/
public static long ageInDays(Date startDate, Date endDate) {
LocalDate sd = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate ed = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return ChronoUnit.DAYS.between(sd, ed);
}
/**
* Calculate days between dates
*
* @param verificationDate
* @return
*/
public static long ageInDays(Date verificationDate) {
return ageInDays(verificationDate, new Date());
}
public static Date truncate(Date dateToTrim) {
Calendar cal = Calendar.getInstance();
cal.setTime(dateToTrim);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* returns truncated date with added days
*
* @param startDate
* @param days
* @return
*/
public static Date trunacateAndAddDays(Date startDate, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.add(Calendar.DAY_OF_YEAR, days);
return truncate(cal.getTime());
}
/**
* add days to current date
*
* @param days
* @return
*/
public static Date addDays(int days) {
return addDays(new Date(), days);
}
/**
* returns date with added days
*
* @param startDate
* @param days
* @return
*/
public static Date addDays(Date startDate, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.add(Calendar.DAY_OF_YEAR, days);
return cal.getTime();
}
/**
* returns date with added minutes
*
* @param startDate
* @param minutes
* @return
*/
public static Date addMinutes(Date startDate, int minutes) {
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
cal.add(Calendar.MINUTE, minutes);
return cal.getTime();
}
/**
* returns date with added minutes
*
* @param minutes
* @return
*/
public static Date addMinutes(int minutes) {
return addMinutes(new Date(), minutes);
}
/**
* Return todays date without time
*
* @return
*/
public static Date shortNow() {
return truncate(new Date());
}
/**
* Returns true if provided date has it's place before today (without time)
*
* @param dateToCheck
* @return
*/
public static Boolean beforeToday(Date dateToCheck) {
return (truncate(dateToCheck).before(shortNow()));
}
/**
* Returns true if provided date has it's place in the future
*
* @param dateToCheck
* @return
*/
public static Boolean inTheFuture(Date dateToCheck) {
return (truncate(dateToCheck).after(shortNow()));
}
public static Date todayWithHourAndMinutes(int hour, int minute) {
Date today = shortNow();
Calendar cal = Calendar.getInstance();
cal.setTime(today);
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hour, minute, 0);
return cal.getTime();
}
/**
* check if date is before provided date; all dates will be truncated to "DAY" before comparison
*
* @param dateToValidate
* @param againsStartDate
* @return
*/
public static Boolean beforeDateTruncated(Date dateToValidate, Date againsStartDate) {
return truncate(dateToValidate).before(truncate(againsStartDate));
}
/**
* Check if we add days to current date, it will be after provided date
*
* @param dateToValidate
* @param daysToAdd
* @return
*/
public static Boolean afterTodayWithDays(Date dateToValidate, int daysToAdd) {
return trunacateAndAddDays(shortNow(), daysToAdd).before(dateToValidate);
}
/**
* get current year
*
* @return
*/
public static long getCurrentYear() {
Calendar now = Calendar.getInstance();
return now.get(Calendar.YEAR);
}
/**
* create XMLGregorianCalendar based on String object
*
* @param date
* @return
* @throws DatatypeConfigurationException
* @throws ParseException
*/
public static XMLGregorianCalendar xmlGregorianCalendarFromDate(String date) throws DatatypeConfigurationException, ParseException {
return xmlGregorianCalendarFromDate(date, "yyyy-MM-dd");
}
public static XMLGregorianCalendar ibsNullDate() {
return DateHelper.xmlGregorianCalendarFromDate(nullDate());
}
/**
* create XMLGregorianCalendar based on String object and string pattern
*
* @param date
* @param dateFormat
* @return
* @throws DatatypeConfigurationException
* @throws ParseException
*/
public static XMLGregorianCalendar xmlGregorianCalendarFromDate(String date, String dateFormat) throws DatatypeConfigurationException, ParseException {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
return xmlGregorianCalendarFromDate(format.parse(date));
}
/**
* create XMLGregorianCalendar based on Date object
*
* @param date
* @return
* @throws DatatypeConfigurationException
*/
public static XMLGregorianCalendar xmlGregorianCalendarFromDate(Date date) {
try {
if (date == null) {
return emptyXmlGregorianCalendar();
}
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
} catch (Exception e) {
return null;
}
}
/**
* get formatted XMLGregorianCalendar based on simple string
*
* @param date
* @return
* @throws DatatypeConfigurationException
*/
public static XMLGregorianCalendar xmlGregorianCalendarSimple(String date, String dateFormat) throws DatatypeConfigurationException, ParseException {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
Date parsed = format.parse(date);
return xmlGregorianCalendarSimple(parsed);
}
/**
* get formatted XMLGregorianCalendar
*
* @param date
* @return
* @throws DatatypeConfigurationException
*/
public static XMLGregorianCalendar xmlGregorianCalendarSimple(Date date) throws DatatypeConfigurationException {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(date);
return DatatypeFactory.newInstance().newXMLGregorianCalendarDate(
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH),
DatatypeConstants.FIELD_UNDEFINED
);
}
/**
* just return empty date
*
* @return
* @throws DatatypeConfigurationException
*/
public static XMLGregorianCalendar emptyXmlGregorianCalendar() throws DatatypeConfigurationException {
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(1000, 0, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
}
public static long daysBetween(Date fromDate, Date toDate) {
return ChronoUnit.DAYS.between(fromDate.toInstant(), toDate.toInstant());
}
/**
* get years amount between two dates
*
* @param fromDate
* @param toDate
* @return
*/
public static int yearsBetween(Date fromDate, Date toDate) {
DateTime from = new DateTime(fromDate.getTime());
DateTime to = new DateTime(toDate.getTime());
return Years.yearsBetween(from, to).getYears();
}
public static Date nullDate() {
return null;
}
public static long currentYear() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
/**
* calculate age of a vehicle, that is round up (eg if its 5 years 3 months, it will be 6 years)
*
* @param firstRegistration date of first registration
* @param insuranceStartingDate starting date of insurance
* @return
*/
public static long vehicleAge(Date firstRegistration, Date insuranceStartingDate) {
Date d = addDays(insuranceStartingDate, -1);
return ageInYears(firstRegistration, d) + 1;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy