Example usage for java.util GregorianCalendar get

List of usage examples for java.util GregorianCalendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:org.sonar.core.computation.dbcleaner.period.Interval.java

static List<Interval> group(List<PurgeableSnapshotDto> snapshots, Date start, Date end, int calendarField) {
    List<Interval> intervals = Lists.newArrayList();

    GregorianCalendar calendar = new GregorianCalendar();
    int lastYear = -1;
    int lastFieldValue = -1;
    Interval currentInterval = null;//w  w  w  .j  ava  2  s.  co  m

    for (PurgeableSnapshotDto snapshot : snapshots) {
        if (!DateUtils.isSameDay(start, snapshot.getDate()) && snapshot.getDate().after(start)
                && (snapshot.getDate().before(end) || DateUtils.isSameDay(end, snapshot.getDate()))) {
            calendar.setTime(snapshot.getDate());
            int currentFieldValue = calendar.get(calendarField);
            int currentYear = calendar.get(Calendar.YEAR);
            if (lastYear != currentYear || lastFieldValue != currentFieldValue) {
                currentInterval = new Interval();
                intervals.add(currentInterval);
            }
            lastFieldValue = currentFieldValue;
            lastYear = currentYear;
            if (currentInterval != null) {
                currentInterval.add(snapshot);
            }
        }
    }
    return intervals;
}

From source file:DateUtils.java

public static final String dateToString(Date dt, String dateformat) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(dt);//from  w w w .j a  va2  s . co m

    StringBuffer ret = new StringBuffer();
    String separator = new String();
    if (dateformat.equals(DateUtils.FORMAT_YYYYMMDD)) {
        separator = "-";
    }
    if (dateformat.equals(DateUtils.FORMAT_YYYYMMDD_SLASHES)) {
        separator = "/";
    }
    ret.append(cal.get(Calendar.YEAR));
    ret.append(separator);
    ret.append(cal.get(Calendar.MONTH) + 1);
    ret.append(separator);
    ret.append(cal.get(Calendar.DATE));

    return ret.toString();
}

From source file:com.clavain.alerts.Methods.java

private static String getScheduleFieldToCheck() {
    GregorianCalendar gc = new GregorianCalendar();
    String retval = "";

    switch (gc.get(Calendar.DAY_OF_WEEK)) {
    case 1://from www  .  jav  a  2  s  .c  om
        retval = "s_mon";
        break;
    case 2:
        retval = "s_tue";
        break;
    case 3:
        retval = "s_wed";
        break;
    case 4:
        retval = "s_thu";
        break;
    case 5:
        retval = "s_fri";
        break;
    case 6:
        retval = "s_sat";
        break;
    case 7:
        retval = "s_sun";
        break;
    }
    return retval;
}

From source file:DateUtils.java

public static final String dateToString(Date dt, String tzString, String dateformat) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(dt);/*from  w  ww  . j av a2s. com*/
    cal.setTimeZone(TimeZone.getTimeZone(tzString));

    StringBuffer ret = new StringBuffer();
    String separator = new String();
    if (dateformat.equals(DateUtils.FORMAT_YYYYMMDD)) {
        separator = "-";
    }
    if (dateformat.equals(DateUtils.FORMAT_YYYYMMDD_SLASHES)) {
        separator = "/";
    }
    ret.append(cal.get(Calendar.YEAR));
    ret.append(separator);
    ret.append(cal.get(Calendar.MONTH) + 1);
    ret.append(separator);
    ret.append(cal.get(Calendar.DATE));

    return ret.toString();
}

From source file:org.tsm.concharto.util.TimeRangeFormat.java

/**
 * Make formatted date more user friendly.  For example, '0092 BC' is converted
 * to '92 BC'/*from  w ww .j a v a  2s  . c  om*/
 * 
 * @param date original date
 * @param text formatted text representation of that date
 * @return stripped text
 */
private static String stripLeadingZeros(Date date, String text) {
    //first we have to find the formatted year.
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    String year = Integer.toString(cal.get(Calendar.YEAR));
    //only do this for four digit years (e.g. no need to do it for 20000 BC)
    if (year.length() <= DIGITS_IN_YEAR) {
        //find the start pos of the year.  If the year is 0093, we will get
        //year = '93' so the start of year = pos of '93' minus 2.
        int yearPos = text.indexOf(year) - DIGITS_IN_YEAR + year.length();
        StringBuffer adjusted = new StringBuffer();
        if (yearPos != 0) {
            adjusted.append(text.substring(0, yearPos));
        }
        adjusted.append(year);
        adjusted.append(text.substring(yearPos + DIGITS_IN_YEAR, text.length()));
        return adjusted.toString();
    } else {
        return text;
    }
}

From source file:com.kcs.core.utilities.Utility.java

public static XMLGregorianCalendar getXMLGregorianCalendarDate(Date date)
        throws DatatypeConfigurationException {
    if (Utility.isNotNull(date)) {
        GregorianCalendar arrgCtrDate = new GregorianCalendar();
        arrgCtrDate.setTime(date);//from w  w  w.j a v a2 s  . co m
        DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
        XMLGregorianCalendar xmlGregorianCalendar = datatypeFactory.newXMLGregorianCalendarDate(
                arrgCtrDate.get(Calendar.YEAR), arrgCtrDate.get(Calendar.MONTH) + 1,
                arrgCtrDate.get(Calendar.DAY_OF_MONTH), DatatypeConstants.FIELD_UNDEFINED);
        return xmlGregorianCalendar;
    }
    return null;
}

From source file:org.novoj.utils.datePattern.DatePatternConverter.java

/**
 * Method returns date object with normalized time. When resetTime = true, time will be set to 0:0:0.0.
 * When date respresents current day (current date) and pattern doesn't contain appropriate pattern parts for
 * hour, minute, second, milliseconds - those values will be set to current time values, otherwise they will be
 * set to initial values (ie. 0)./*from w w w. j  a  v a  2 s .  co m*/
 */
private static Date normalizeHours(Calendar nowCld, Date date, Locale locale, String pattern,
        boolean resetTime) {
    GregorianCalendar dateCld = new GregorianCalendar();
    dateCld.setTime(date);
    boolean currentYear = nowCld.get(Calendar.YEAR) == dateCld.get(Calendar.YEAR);
    boolean currentMonth = nowCld.get(Calendar.MONTH) == dateCld.get(Calendar.MONTH);
    boolean currentDay = nowCld.get(Calendar.DAY_OF_MONTH) == dateCld.get(Calendar.DAY_OF_MONTH);
    //noinspection OverlyComplexBooleanExpression
    if (!resetTime && currentYear && currentMonth && currentDay) {
        //set current time
        if (pattern.toLowerCase(locale).indexOf('h') == -1) {
            dateCld.set(Calendar.HOUR, nowCld.get(Calendar.HOUR));
        }
        if (pattern.indexOf('m') == -1) {
            dateCld.set(Calendar.MINUTE, nowCld.get(Calendar.MINUTE));
        }
        if (pattern.indexOf('s') == -1) {
            dateCld.set(Calendar.SECOND, nowCld.get(Calendar.SECOND));
        }
        if (pattern.indexOf('S') == -1) {
            dateCld.set(Calendar.MILLISECOND, nowCld.get(Calendar.MILLISECOND));
        }
    } else {
        //set zero time
        if (pattern.toLowerCase(locale).indexOf('h') == -1) {
            dateCld.set(Calendar.HOUR, 0);
        }
        if (pattern.indexOf('m') == -1) {
            dateCld.set(Calendar.MINUTE, 0);
        }
        if (pattern.indexOf('s') == -1) {
            dateCld.set(Calendar.SECOND, 0);
        }
        if (pattern.indexOf('S') == -1) {
            dateCld.set(Calendar.MILLISECOND, 0);
        }
    }
    return dateCld.getTime();
}

From source file:org.tsm.concharto.util.TimeRangeFormat.java

/**
 * evaluates whether the separation between the begin and end is equal to 'separation' parameter
 * @param calendarField calendar field (e.g. Calendar.MONTH)
 * @param separation number of places of separation
 * @param tr SimpleTimeRange//from  w  w  w.  j a va  2s . c  o m
 * @return true if begin-end = separation for the given calendar field (e.g. Calendar.MONTH)
 */
private static boolean isSeparatedBy(int calendarField, int separation, SimpleTimeRange tr) {
    GregorianCalendar begin = getCalendar(tr.getBegin().getDate());
    GregorianCalendar end = getCalendar(tr.getEnd().getDate());
    //roll begin by the separation ammount (takes into account boundaries e.g. month 12 + 1 = month 1) 
    if (calendarField == Calendar.YEAR) {
        if (end.get(Calendar.ERA) == GregorianCalendar.BC) {
            separation = -separation;
        }
    }
    begin.roll(calendarField, separation);
    int endField = end.get(calendarField);
    int beginField = begin.get(calendarField);

    return (0 == (endField - beginField));
}

From source file:org.tsm.concharto.util.TimeRangeFormat.java

/** 
 * format a date/*from w  w  w  .  j a  va  2 s.  co  m*/
 * @param date date
 * @param cp CalendarPrecision for getting one of the format strings
 * @return String formatted string
 */
private static String dateFormat(Date date, CalendarPrecision cp) {
    String format;
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    //if the year is an AD date and it is pretty old (e.g. less than 1000AD), then append the era
    //always display the era for BC dates
    if ((cal.get(Calendar.ERA) == GregorianCalendar.BC) || (cal.get(Calendar.YEAR) < MAX_YEAR_TO_DISLPAY_ERA)) {
        format = cp.getFormatWithEra();
    } else {
        format = cp.getFormat();
    }

    String text = DateFormatUtils.format(date, format);
    return stripLeadingZeros(date, text);
}

From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.DataTypeUtils.java

/**
 * @param pid/*from  w w w .  j ava  2s .  c  o m*/
 * @param pidNumber pass in the # of this pid for the sequence, i.e. normally it's 1 if you only have 1 pid entry. if this is a list of pids, then the first one is 1, second is 2 etc..
 * @param demographic
 * @throws HL7Exception
 */
public static void fillPid(PID pid, int pidNumber, Demographic demographic) throws HL7Exception {
    // defined as first pid=1 second pid=2 etc
    pid.getSetIDPID().setValue(String.valueOf(pidNumber));

    CX cx = pid.getPatientIdentifierList(0);
    // health card string, excluding version code
    cx.getIDNumber().setValue(demographic.getHin());
    cx.getIdentifierTypeCode().setValue(HEALTH_NUMBER);
    // blank for everyone but ontario use version code
    cx.getIdentifierCheckDigit().setValue(demographic.getVer());
    // province
    cx.getAssigningJurisdiction().getIdentifier().setValue(demographic.getHcType());

    GregorianCalendar tempCalendar = new GregorianCalendar();
    if (demographic.getEffDate() != null) {
        tempCalendar.setTime(demographic.getEffDate());
        cx.getEffectiveDate().setYearMonthDayPrecision(tempCalendar.get(GregorianCalendar.YEAR),
                tempCalendar.get(GregorianCalendar.MONTH) + 1,
                tempCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }

    if (demographic.getHcRenewDate() != null) {
        tempCalendar.setTime(demographic.getHcRenewDate());
        cx.getExpirationDate().setYearMonthDayPrecision(tempCalendar.get(GregorianCalendar.YEAR),
                tempCalendar.get(GregorianCalendar.MONTH) + 1,
                tempCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }

    XPN xpn = pid.getPatientName(0);
    xpn.getFamilyName().getSurname().setValue(demographic.getLastName());
    xpn.getGivenName().setValue(demographic.getFirstName());
    // Value Description
    // -----------------
    // A Alias Name
    // B Name at Birth
    // C Adopted Name
    // D Display Name
    // I Licensing Name
    // L Legal Name
    // M Maiden Name
    // N Nickname /Call me Name/Street Name
    // P Name of Partner/Spouse - obsolete (DO NOT USE)
    // R Registered Name (animals only)
    // S Coded Pseudo-Name to ensure anonymity
    // T Indigenous/Tribal/Community Name
    // U Unspecified
    xpn.getNameTypeCode().setValue("L");

    if (demographic.getBirthDay() != null) {
        DTM bday = pid.getDateTimeOfBirth();
        tempCalendar = demographic.getBirthDay();
        bday.setDatePrecision(tempCalendar.get(GregorianCalendar.YEAR),
                tempCalendar.get(GregorianCalendar.MONTH) + 1,
                tempCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }

    // Value Description
    // -----------------
    // F Female
    // M Male
    // O Other
    // U Unknown
    // A Ambiguous
    // N Not applicable
    pid.getAdministrativeSex().setValue(getHl7GenderFromOscarGender(demographic.getSex()));

    XAD address = pid.getPatientAddress(0);
    fillXAD(address, demographic, null, "H");

    XTN phone = pid.getPhoneNumberHome(0);
    phone.getUnformattedTelephoneNumber().setValue(demographic.getPhone());

    // ISO 639, hrmmm shall we say 639-1 (2 digit)?
    pid.getPrimaryLanguage().getIdentifier().setValue(demographic.getSpokenLanguage());

    // ISO table 3166.
    pid.getCitizenship(0).getIdentifier().setValue(demographic.getCitizenship());
}