Java Code Examples for org.apache.poi.util.LocaleUtil#getLocaleCalendar()

The following examples show how to use org.apache.poi.util.LocaleUtil#getLocaleCalendar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateUtil.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get EXCEL date as Java Calendar with given time zone.
 * @param date  The Excel date.
 * @param use1904windowing  true if date uses 1904 windowing,
 *  or false if using 1900 date windowing.
 * @param timeZone The TimeZone to evaluate the date in
 * @param roundSeconds round to closest second
 * @return Java representation of the date, or null if date is not a valid Excel date
 */
public static Calendar getJavaCalendar(double date, boolean use1904windowing, TimeZone timeZone, boolean roundSeconds) {
    if (!isValidExcelDate(date)) {
        return null;
    }
    int wholeDays = (int)Math.floor(date);
    int millisecondsInDay = (int)((date - wholeDays) * DAY_MILLISECONDS + 0.5);
    Calendar calendar;
    if (timeZone != null) {
        calendar = LocaleUtil.getLocaleCalendar(timeZone);
    } else {
        calendar = LocaleUtil.getLocaleCalendar(); // using default time-zone
    }
    setCalendar(calendar, wholeDays, millisecondsInDay, use1904windowing, roundSeconds);
    return calendar;
}
 
Example 2
Source File: WorkdayCalculator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
    * Calculate the workday past x workdays from a starting date, considering a range of holidays.
    *
    * @param start start date.
    * @param workdays number of workdays to be past from starting date.
    * @param holidays an array of holidays.
    * @return date past x workdays.
    */
public Date calculateWorkdays(double start, int workdays, double[] holidays) {
	Date startDate = DateUtil.getJavaDate(start);
	int direction = workdays < 0 ? -1 : 1;
	Calendar endDate = LocaleUtil.getLocaleCalendar();
	endDate.setTime(startDate);
	double excelEndDate = DateUtil.getExcelDate(endDate.getTime());
	while (workdays != 0) {
		endDate.add(Calendar.DAY_OF_YEAR, direction);
		excelEndDate += direction;
		if (endDate.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
				&& endDate.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
				&& !isHoliday(excelEndDate,	holidays)) {
			workdays -= direction;
		}
	}
	return endDate.getTime();
}
 
Example 3
Source File: EDate.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    if (args.length != 2) {
        return ErrorEval.VALUE_INVALID;
    }
    try {
        double startDateAsNumber = getValue(args[0]);
        int offsetInMonthAsNumber = (int) getValue(args[1]);

        Date startDate = DateUtil.getJavaDate(startDateAsNumber);
        Calendar calendar = LocaleUtil.getLocaleCalendar();
        calendar.setTime(startDate);
        calendar.add(Calendar.MONTH, offsetInMonthAsNumber);
        return new NumberEval(DateUtil.getExcelDate(calendar.getTime()));
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
 
Example 4
Source File: DateUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static Date parseYYYYMMDDDateInternal(String timeStr) throws FormatException {
    if(timeStr.length() != 10) {
        throw new FormatException("Bad length");
    }

    String yearStr = timeStr.substring(0, 4);
    String monthStr = timeStr.substring(5, 7);
    String dayStr = timeStr.substring(8, 10);
    int year = parseInt(yearStr, "year", Short.MIN_VALUE, Short.MAX_VALUE);
    int month = parseInt(monthStr, "month", 1, 12);
    int day = parseInt(dayStr, "day", 1, 31);

    Calendar cal = LocaleUtil.getLocaleCalendar(year, month-1, day);
    return cal.getTime();
}
 
Example 5
Source File: DateParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @param month 1-based
 */
private static Calendar makeDate(int year, int month, int day) throws EvaluationException {
    if (month < 1 || month > 12) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    Calendar cal = LocaleUtil.getLocaleCalendar(year, month - 1, 1, 0, 0, 0);
    if (day < 1 || day > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
        throw new EvaluationException(ErrorEval.VALUE_INVALID);
    }
    cal.set(Calendar.DAY_OF_MONTH, day);
    return cal;
}
 
Example 6
Source File: Today.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex) {
	Calendar now = LocaleUtil.getLocaleCalendar();
	now.clear(Calendar.HOUR);
       now.set(Calendar.HOUR_OF_DAY,0);
	now.clear(Calendar.MINUTE);
	now.clear(Calendar.SECOND);
	now.clear(Calendar.MILLISECOND);
	return new NumberEval(DateUtil.getExcelDate(now.getTime()));
}
 
Example 7
Source File: EOMonth.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    if (args.length != 2) {
        return ErrorEval.VALUE_INVALID;
    }

    try {
        double startDateAsNumber = NumericFunction.singleOperandEvaluate(args[0], ec.getRowIndex(), ec.getColumnIndex());
        int months = (int) NumericFunction.singleOperandEvaluate(args[1], ec.getRowIndex(), ec.getColumnIndex());

        // Excel treats date 0 as 1900-01-00; EOMONTH results in 1900-01-31
        if (startDateAsNumber >= 0.0 && startDateAsNumber < 1.0) {
            startDateAsNumber = 1.0;
        }

        Date startDate = DateUtil.getJavaDate(startDateAsNumber, false);

        Calendar cal = LocaleUtil.getLocaleCalendar();
        cal.setTime(startDate);
        cal.clear(Calendar.HOUR);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.clear(Calendar.MINUTE);
        cal.clear(Calendar.SECOND);
        cal.clear(Calendar.MILLISECOND);

        cal.add(Calendar.MONTH, months + 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);

        return new NumberEval(DateUtil.getExcelDate(cal.getTime()));
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
 
Example 8
Source File: WorkdayCalculator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @param aDate a given date.
 * @return <code>true</code> if date is weekend, <code>false</code> otherwise.
 */
protected boolean isWeekend(double aDate) {
    Calendar date = LocaleUtil.getLocaleCalendar();
    date.setTime(DateUtil.getJavaDate(aDate));
    return date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
}
 
Example 9
Source File: YearFracCalculator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static SimpleDate createDate(int dayCount) {
    /** use UTC time-zone to avoid daylight savings issues */
	Calendar cal = LocaleUtil.getLocaleCalendar(LocaleUtil.TIMEZONE_UTC);
	DateUtil.setCalendar(cal, dayCount, 0, false, false);
	return new SimpleDate(cal);
}
 
Example 10
Source File: DateFunc.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Note - works with Java Calendar months, not Excel months
 */
private static double evaluate(int year, int month, int pDay) throws EvaluationException {
   // We don't support negative years yet
	if (year < 0) {
		throw new EvaluationException(ErrorEval.VALUE_INVALID);
	}
	// Negative months are fairly easy
	while (month < 0) {
	   year--;
	   month += 12;
	}
	// Negative days are handled by the Java Calendar
	
	// Excel has bugs around leap years in 1900, handle them
	// Special case for the non-existant 1900 leap year
	if (year == 1900 && month == Calendar.FEBRUARY && pDay == 29) {
		return 60.0;
	}

	// If they give a date in 1900 in Jan/Feb, with the days
	//  putting it past the leap year, adjust
	int day = pDay;
	if (year == 1900) {
		if ((month == Calendar.JANUARY && day >= 60) ||
				(month == Calendar.FEBRUARY && day >= 30)) {
			day--;
		}
	}

	// Turn this into a Java date
	Calendar c = LocaleUtil.getLocaleCalendar(year, month, day);
	
	// Handle negative days of the week, that pull us across
	//  the 29th of Feb 1900
	if (pDay < 0 && c.get(Calendar.YEAR) == 1900 &&
	      month > Calendar.FEBRUARY && 
	      c.get(Calendar.MONTH) < Calendar.MARCH) {
	   c.add(Calendar.DATE, 1);
	}

	// TODO Identify if we're doing 1900 or 1904 date windowing
	boolean use1904windowing = false;
	
	// Have this Java date turned back into an Excel one
	return DateUtil.getExcelDate(c.getTime(), use1904windowing);
}
 
Example 11
Source File: Days360.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static Calendar getDate(double date) {
    Calendar processedDate = LocaleUtil.getLocaleCalendar();
    processedDate.setTime(DateUtil.getJavaDate(date, false));
    return processedDate;
}
 
Example 12
Source File: DateUtil.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Given a Date, converts it into a double representing its internal Excel representation,
 *   which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds.
 *
 * @return Excel representation of Date (-1 if error - test for error by checking for less than 0.1)
 * @param date the Date
 * @param use1904windowing Should 1900 or 1904 date windowing be used?
 */
public static double getExcelDate(Date date, boolean use1904windowing) {
    Calendar calStart = LocaleUtil.getLocaleCalendar();
    calStart.setTime(date);   // If date includes hours, minutes, and seconds, set them to 0
    return internalGetExcelDate(calStart, use1904windowing);
}