Example usage for java.util Calendar clear

List of usage examples for java.util Calendar clear

Introduction

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

Prototype

public final void clear() 

Source Link

Document

Sets all the calendar field values and the time value (millisecond offset from the Epoch) of this Calendar undefined.

Usage

From source file:DateParser.java

private static Calendar getCalendar(String isodate) {
    // YYYY-MM-DDThh:mm:ss.sTZD
    StringTokenizer st = new StringTokenizer(isodate, "-T:.+Z", true);

    Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.clear();
    try {// w  w  w .  java2  s  . c om
        // Year
        if (st.hasMoreTokens()) {
            int year = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.YEAR, year);
        } else {
            return calendar;
        }
        // Month
        if (check(st, "-") && (st.hasMoreTokens())) {
            int month = Integer.parseInt(st.nextToken()) - 1;
            calendar.set(Calendar.MONTH, month);
        } else {
            return calendar;
        }
        // Day
        if (check(st, "-") && (st.hasMoreTokens())) {
            int day = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.DAY_OF_MONTH, day);
        } else {
            return calendar;
        }
        // Hour
        if (check(st, "T") && (st.hasMoreTokens())) {
            int hour = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.HOUR_OF_DAY, hour);
        } else {
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }
        // Minutes
        if (check(st, ":") && (st.hasMoreTokens())) {
            int minutes = Integer.parseInt(st.nextToken());
            calendar.set(Calendar.MINUTE, minutes);
        } else {
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            return calendar;
        }

        //
        // Not mandatory now
        //

        // Secondes
        if (!st.hasMoreTokens()) {
            return calendar;
        }
        String tok = st.nextToken();
        if (tok.equals(":")) { // secondes
            if (st.hasMoreTokens()) {
                int secondes = Integer.parseInt(st.nextToken());
                calendar.set(Calendar.SECOND, secondes);
                if (!st.hasMoreTokens()) {
                    return calendar;
                }
                // frac sec
                tok = st.nextToken();
                if (tok.equals(".")) {
                    // bug fixed, thx to Martin Bottcher
                    String nt = st.nextToken();
                    while (nt.length() < 3) {
                        nt += "0";
                    }
                    nt = nt.substring(0, 3); // Cut trailing chars..
                    int millisec = Integer.parseInt(nt);
                    // int millisec = Integer.parseInt(st.nextToken()) * 10;
                    calendar.set(Calendar.MILLISECOND, millisec);
                    if (!st.hasMoreTokens()) {
                        return calendar;
                    }
                    tok = st.nextToken();
                } else {
                    calendar.set(Calendar.MILLISECOND, 0);
                }
            } else {
                throw new RuntimeException("No secondes specified");
            }
        } else {
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
        }
        // Timezone
        if (!tok.equals("Z")) { // UTC
            if (!(tok.equals("+") || tok.equals("-"))) {
                throw new RuntimeException("only Z, + or - allowed");
            }
            boolean plus = tok.equals("+");
            if (!st.hasMoreTokens()) {
                throw new RuntimeException("Missing hour field");
            }
            int tzhour = Integer.parseInt(st.nextToken());
            int tzmin = 0;
            if (check(st, ":") && (st.hasMoreTokens())) {
                tzmin = Integer.parseInt(st.nextToken());
            } else {
                throw new RuntimeException("Missing minute field");
            }
            if (plus) {
                calendar.add(Calendar.HOUR, -tzhour);
                calendar.add(Calendar.MINUTE, -tzmin);
            } else {
                calendar.add(Calendar.HOUR, tzhour);
                calendar.add(Calendar.MINUTE, tzmin);
            }
        }
    } catch (NumberFormatException ex) {
        throw new RuntimeException("[" + ex.getMessage() + "] is not an integer");
    }
    return calendar;
}

From source file:com.example.geomesa.transformations.QueryTutorial.java

/**
 * Creates a base filter that will return a small subset of our results. This can be tweaked to
 * return different results if desired. Currently it should return 16 results.
 *
 * @return//from w ww  .  j  a va2  s  .  c  o  m
 *
 * @throws CQLException
 * @throws IOException
 */
static Filter createBaseFilter() throws CQLException, IOException {

    // Get a FilterFactory2 to build up our query
    FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();

    // We are going to query for events in Ukraine during the
    // civil unrest.

    // We'll start by looking at a particular day in February of 2014
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.YEAR, 2014);
    calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
    calendar.set(Calendar.DAY_OF_MONTH, 2);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    Date start = calendar.getTime();

    calendar.set(Calendar.HOUR_OF_DAY, 23);
    Date end = calendar.getTime();

    Filter timeFilter = ff.between(ff.property(GdeltFeature.Attributes.SQLDATE.getName()), ff.literal(start),
            ff.literal(end));

    // We'll bound our query spatially to Ukraine
    Filter spatialFilter = ff.bbox(GdeltFeature.Attributes.geom.getName(), 22.1371589, 44.386463, 40.228581,
            52.379581, "EPSG:4326");

    // we'll also restrict our query to only articles about the US, UK or UN
    Filter attributeFilter = ff.like(ff.property(GdeltFeature.Attributes.Actor1Name.getName()), "UNITED%");

    // Now we can combine our filters using a boolean AND operator
    Filter conjunction = ff.and(Arrays.asList(timeFilter, spatialFilter, attributeFilter));

    return conjunction;
}

From source file:org.hyperic.hq.measurement.shared.MeasTabManagerUtil.java

public static String getMeasTabname(Calendar cal, long timems) {
    int dayofperiod = getDayOfPeriod(cal, timems);
    cal.clear();
    cal.setTime(new java.util.Date(timems));
    _log.debug("dayofperiod -> " + dayofperiod);
    int hourofday = cal.get(Calendar.HOUR_OF_DAY);
    int numdaytables = NUMBER_OF_TABLES / NUMBER_OF_TABLES_PER_DAY;
    int daytable = dayofperiod % numdaytables;
    int dayincr = 24 / NUMBER_OF_TABLES_PER_DAY;
    int dayslice = 0;
    for (int i = dayincr; i < 24; i += dayincr) {
        if (hourofday < i) {
            break;
        }//from w w w .j a  va2 s.c  o m
        dayslice++;
    }
    return MEAS_TABLE + "_" + daytable + "D_" + dayslice + "S";
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * /* www  .j  a  va  2s . c o  m*/
 *
 * @return
 */
public static String getNextMonthFirst() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    // f.set(Calendar.DATE, 1);
    // or f.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    cal.set(Calendar.DATE, 1);// ?1?
    cal.add(Calendar.MONTH, +1);// ?1?
    return DateFormatUtils.format(cal, DATE_FORMAT);
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * //from w ww. j ava 2 s  .  c  om
 *
 * @return
 */
public static String getPreviousMonthFirst() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    // f.set(Calendar.DATE, 1);
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    // f.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // (?)
    cal.set(Calendar.DATE, 1);// ?1?
    cal.add(Calendar.MONTH, -1);
    return DateFormatUtils.format(cal, DATE_FORMAT);
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ??//from   w w w  . j  av  a2s  .c  o  m
 *
 * @return
 */
public static String getMonthLastDay() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
    // f.set(Calendar.MILLISECOND, -1);
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    // f.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // (?)
    cal.set(Calendar.DATE, 1);// ?1?
    cal.add(Calendar.MONTH, 1);// ?1?
    cal.add(Calendar.DATE, -1);// ???
    return DateFormatUtils.format(cal, DATE_FORMAT);
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ?/*  www  .  ja  va2s .c o m*/
 *
 * @return
 */
public static String getPreviousMonthEnd() {
    Calendar cal = Calendar.getInstance();
    Calendar f = (Calendar) cal.clone();
    f.clear();
    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    // f.set(Calendar.MILLISECOND, -1);
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // 
    // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
    // f.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
    // return DateFormatUtils.format(f, DATE_FORMAT);

    // (?)
    cal.set(Calendar.DATE, 1);// ?1?
    cal.add(Calendar.MONTH, 0);//
    cal.add(Calendar.DATE, -1);// ???
    return DateFormatUtils.format(cal, DATE_FORMAT);
}

From source file:org.appcelerator.util.DateUtil.java

/**
 * gets today as a date//from  ww  w.ja va2s  .c om
 *
 * @return today as date
 */
public static Date today() {
    Calendar calendar = new GregorianCalendar(GMT_TZ);
    calendar.setTime(new Date());

    Calendar today = new GregorianCalendar(GMT_TZ);
    today.clear();
    today.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
    return today.getTime();
}

From source file:org.openvpms.web.resource.i18n.format.DateFormatter.java

/**
 * Parse a time from a string, expected to be in the range 0..24.
 *
 * @param value   the string to parse//from  ww w . j  ava  2 s.  co  m
 * @param allow24 if {@code true}, allow hours in the range 0..24, else 0..23
 * @return the parsed time
 * @throws NumberFormatException    if the string is not a valid no
 * @throws IllegalArgumentException if the hours aren't in the range 0..24
 */
private static Date parseHours(String value, boolean allow24) {
    int hours = getHours(value, allow24);
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(Calendar.HOUR_OF_DAY, hours);
    return calendar.getTime();
}

From source file:com.tesora.dve.db.mysql.common.MysqlAPIUtils.java

/**
 * Methods to read and write length coded times in the Binary Protocol
 * /*from w ww . j  a  v a  2 s. c o m*/
 * if days, hours, minutes, seconds and micro_seconds are all 0, length is 0
 * and no other field is sent
 * 
 * if micro_seconds is 0, length is 8 and micro_seconds is not sent
 * otherwise length is 12
 * 
 * Fields 
 *       length (1) -- number of bytes following (valid values: 0, 8, 12)
 *       is_negative (1) -- (1 if minus, 0 for plus) 
 *       days (4) -- days 
 *       hours (1) -- hours
 *       minutes (1) -- minutes
 *       seconds (1) -- seconds
 *       micro_seconds (4) -- micro-seconds
 */
public static Time getLengthCodedTime(ByteBuf cb) throws PEException {
    Calendar cal = Calendar.getInstance();
    cal.clear();
    short length = cb.readUnsignedByte();

    if (length == 0)
        return null;

    Time ret = null;

    if (length >= 8) {
        cb.skipBytes(1); // this is the sign - we are ignoring this for now
        cb.skipBytes(4); // this is "days" - we are ignoring this for now
        cal.set(Calendar.HOUR_OF_DAY, cb.readByte());
        cal.set(Calendar.MINUTE, cb.readByte());
        cal.set(Calendar.SECOND, cb.readByte());
        if (length == 12) {
            long microSeconds = cb.readUnsignedInt();
            cal.set(Calendar.MILLISECOND, (int) (microSeconds / 1000));
        }
        if (length > 12) {
            throw new PEException("Invalid length specified to date type (" + length + ")");
        }

        ret = new Time(cal.getTimeInMillis());
    }
    return ret;
}