Java Utililty Methods Day End

List of utility methods to do Day End

Description

The list of methods to do Day End are organized into topic(s).

Method

intabsoluteDay(Calendar cal)
Given a Calendar, return the number of days since 1600/12/31.
return cal.get(Calendar.DAY_OF_YEAR) + daysInPriorYears(cal.get(Calendar.YEAR));
DatecalculateMaxStartDate(Calendar calendar, int daysAhead)
calculate Max Start Date
calendar.add(Calendar.DAY_OF_YEAR, daysAhead);
return calendar.getTime();
floatcalcYears(Date startDate, Date endDate)
calc Years
Calendar startCalendar = Calendar.getInstance();
startCalendar.setTime(startDate);
Calendar endCalendar = Calendar.getInstance();
endCalendar.setTime(endDate);
long time = endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis();
float years = time / 1000.0f / 60.0f / 60.0f / 24.0f / 365.0f;
return years;
CalendarcalenderFromDateString(String dateAndTime)
Transforms a date and time string of the format:
dd/mm/yyyy hh:mm:ss
to a calendar object.
String[] dateTime = dateAndTime.trim().split(" ");
String datetoken = dateTime[0];
String timetoken = dateTime[1];
String[] startDay = datetoken.trim().split("/");
String[] startHour = timetoken.trim().split(":");
return new GregorianCalendar(Integer.parseInt(startDay[2]), Integer.parseInt(startDay[1]) - 1,
        Integer.parseInt(startDay[0]), Integer.parseInt(startHour[0]), Integer.parseInt(startHour[1]),
        Integer.parseInt(startHour[2]));
...
booleancheckTimeInRangeWithSkew(Date timeToCheck, Date startDate, Date endDate, int skewInMinutes)
Checks that a date falls in the interval allowing for a certain clock skew expressed in minutes.
if (startDate.after(endDate) || startDate.equals(endDate)) {
    String msg = String.format(
            "Illegal time interval: start date must be before end date. [start date: %s, end date: %s]",
            startDate, endDate);
    throw new IllegalArgumentException(msg);
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
...
booleancheckTimePeriod(final Date beginHour, final Date endHour, final Date hour)
Checks time period.
final Calendar calendarBegin = Calendar.getInstance();
calendarBegin.setTime(beginHour);
final Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(endHour);
final Calendar calendarTime = Calendar.getInstance();
calendarTime.setTime(hour);
boolean betweenPeriod = false;
calendarBegin.set(Calendar.DAY_OF_MONTH, 1);
...
longcountDaysBetween(Date start, Date end)
Calculates the number of days between start and end dates, taking into consideration leap years, year boundaries etc.
if (end.before(start)) {
    throw new IllegalArgumentException("The end date must be later than the start date");
Calendar startCal = GregorianCalendar.getInstance();
startCal.setTime(start);
startCal.set(Calendar.HOUR_OF_DAY, 0);
startCal.set(Calendar.MINUTE, 0);
startCal.set(Calendar.SECOND, 0);
...
intcountMonths(java.util.Date startDate, java.util.Date endDate)
count Months
cal1.setTime(endDate);
cal2.setTime(startDate);
return ((cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12) + cal1.get(Calendar.MONTH)
        - cal2.get(Calendar.MONTH);
ListdateBetweens(Date start, Date end)
date Betweens
List<Date> res = new ArrayList<Date>();
Calendar startCal = getCalendar(start);
while (startCal.getTime().before(end)) {
    Date resultado = startCal.getTime();
    res.add(resultado);
    startCal.add(Calendar.DATE, 1);
return res;
...
int[]dateSpan(Date beginDate, Date endDate)
date Span
Calendar b_cal = Calendar.getInstance();
b_cal.setTime(beginDate);
Calendar e_cal = Calendar.getInstance();
e_cal.setTime(endDate);
int year = e_cal.get(Calendar.YEAR) - b_cal.get(Calendar.YEAR);
int month = e_cal.get(Calendar.MONTH) - b_cal.get(Calendar.MONTH);
if (month < 0 && year > 0) {
    year--;
...