Example usage for java.util GregorianCalendar GregorianCalendar

List of usage examples for java.util GregorianCalendar GregorianCalendar

Introduction

In this page you can find the example usage for java.util GregorianCalendar GregorianCalendar.

Prototype

public GregorianCalendar(Locale aLocale) 

Source Link

Document

Constructs a GregorianCalendar based on the current time in the default time zone with the given locale.

Usage

From source file:com.aurel.track.fieldType.runtime.matchers.run.DateMatcherRT.java

/**
 * Reinitialize dayBegin and dayEnd// ww w .  j  a v  a2  s  . co  m
 */
@Override
public void setMatchValue(Object matcherValue) {
    super.setMatchValue(matcherValue);
    if (relation == MatchRelations.LATER_AS_LASTLOGIN && matcherContext != null) {
        matcherValue = matcherContext.getLastLoggedDate();
    }
    Calendar calendaMatcherValue = new GregorianCalendar(matcherContext.getLocale());
    calendaMatcherValue.setTime(new Date());
    calendaMatcherValue = CalendarUtil.clearTime(calendaMatcherValue);
    int firstDayOfWeek = calendaMatcherValue.getFirstDayOfWeek();
    switch (relation) {
    case MatchRelations.THIS_WEEK:
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayBegin = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.WEEK_OF_YEAR, 1);
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayEnd = calendaMatcherValue.getTime();
        return;
    case MatchRelations.LAST_WEEK:
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayEnd = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.WEEK_OF_YEAR, -1);
        calendaMatcherValue.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        dayBegin = calendaMatcherValue.getTime();
        return;
    case MatchRelations.THIS_MONTH:
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayBegin = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.MONTH, 1);
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayEnd = calendaMatcherValue.getTime();
        return;
    case MatchRelations.LAST_MONTH:
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayEnd = calendaMatcherValue.getTime();
        calendaMatcherValue.add(Calendar.MONTH, -1);
        calendaMatcherValue.set(Calendar.DAY_OF_MONTH, 1);
        dayBegin = calendaMatcherValue.getTime();
        return;
    }

    if (matcherValue == null) {
        return;
    }
    //reinitialize dayBegin and dayEnd
    Date matcherValueDate = null;
    Integer matcherValueInteger = null;
    switch (relation) {
    case MatchRelations.MORE_THAN_DAYS_AGO:
    case MatchRelations.MORE_THAN_EQUAL_DAYS_AGO:
    case MatchRelations.LESS_THAN_DAYS_AGO:
    case MatchRelations.LESS_THAN_EQUAL_DAYS_AGO:
        try {
            matcherValueInteger = (Integer) matchValue;
        } catch (Exception e) {
            LOGGER.warn("Converting the matcher value " + matchValue + " of type "
                    + matchValue.getClass().getName() + " to Integer failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        calendaMatcherValue.setTime(new Date());
        if (matcherValueInteger != null) {
            int matcherValueInt = matcherValueInteger.intValue();
            if (relation == MatchRelations.MORE_THAN_EQUAL_DAYS_AGO) {
                matcherValueInt--;
            }
            if (relation == MatchRelations.LESS_THAN_EQUAL_DAYS_AGO) {
                matcherValueInt++;
            }
            calendaMatcherValue.add(Calendar.DATE, -matcherValueInt);
        }
        break;
    case MatchRelations.IN_MORE_THAN_DAYS:
    case MatchRelations.IN_MORE_THAN_EQUAL_DAYS:
    case MatchRelations.IN_LESS_THAN_DAYS:
    case MatchRelations.IN_LESS_THAN_EQUAL_DAYS:
        try {
            matcherValueInteger = (Integer) matchValue;
        } catch (Exception e) {
            LOGGER.warn("Converting the matcher value " + matchValue + " of type "
                    + matchValue.getClass().getName() + " to Integer failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        calendaMatcherValue.setTime(new Date());
        if (matcherValueInteger != null) {
            int matcherValueInt = matcherValueInteger.intValue();
            if (relation == MatchRelations.IN_MORE_THAN_EQUAL_DAYS) {
                matcherValueInt--;
            }
            if (relation == MatchRelations.IN_LESS_THAN_EQUAL_DAYS) {
                matcherValueInt++;
            }
            calendaMatcherValue.add(Calendar.DATE, matcherValueInt);
        }
        break;
    case MatchRelations.EQUAL_DATE:
    case MatchRelations.NOT_EQUAL_DATE:
    case MatchRelations.GREATHER_THAN_DATE:
    case MatchRelations.GREATHER_THAN_EQUAL_DATE:
    case MatchRelations.LESS_THAN_DATE:
    case MatchRelations.LESS_THAN_EQUAL_DATE:
        try {
            matcherValueDate = (Date) matchValue;
        } catch (Exception e) {
            LOGGER.warn("Converting the matcher value " + matchValue + " of type "
                    + matchValue.getClass().getName() + " to Date failed with " + e.getMessage());
            LOGGER.debug(ExceptionUtils.getStackTrace(e));
        }
        if (matcherValueDate != null) {
            calendaMatcherValue.setTime(matcherValueDate);
            if (relation == MatchRelations.GREATHER_THAN_EQUAL_DATE) {
                calendaMatcherValue.add(Calendar.DATE, -1);
            } else {
                if (relation == MatchRelations.LESS_THAN_EQUAL_DATE) {
                    calendaMatcherValue.add(Calendar.DATE, 1);
                }
            }
        }
        break;
    case MatchRelations.LATER_AS_LASTLOGIN:
        if (matcherContext != null) {
            dayBegin = matcherContext.getLastLoggedDate();
            dayEnd = matcherContext.getLastLoggedDate();
        }
        //return because later the dayBegin and dayEnd will be assigned again
        return;
    }

    calendaMatcherValue = CalendarUtil.clearTime(calendaMatcherValue);
    dayBegin = calendaMatcherValue.getTime();
    calendaMatcherValue.add(Calendar.DATE, 1);
    dayEnd = calendaMatcherValue.getTime();
}

From source file:de.betterform.xml.xforms.xpath.saxon.function.xpath.FileDate.java

private String formatDateString(long modified, String format) {
    Calendar calendar = new GregorianCalendar(Locale.getDefault());
    calendar.setTimeInMillis(modified);/*from   w  w w  .  j  ava2 s.c  o m*/
    SimpleDateFormat simple = null;
    String result;
    if (format == null || format.equals("")) {
        //default format
        simple = new SimpleDateFormat("dd.MM.yyyy H:m:s");
    } else {
        //custom format
        try {
            simple = new SimpleDateFormat(format);
        } catch (IllegalArgumentException e) {
            //                result = "Error: illegal Date format string";
            //todo: do something better
        }
    }
    result = simple.format(calendar.getTime());
    return result;
}