Example usage for java.util Calendar add

List of usage examples for java.util Calendar add

Introduction

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

Prototype

public abstract void add(int field, int amount);

Source Link

Document

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:com.krawler.common.util.SchedulingUtilities.java

public static Date calculateEndDate(Date sDate, String duration, int[] NonWorkDays, String[] CmpHoliDays,
        String userid) throws ParseException, ServiceException {
    Date dt = new Date();
    java.text.SimpleDateFormat sdfLong = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String edate = "";
    Calendar c = Calendar.getInstance();
    dt = sDate;//from ww  w.j  ava 2 s.  c o m
    double d = getDurationInDays(duration);
    c.setTime(dt);
    if (d > 0) {
        c.add(Calendar.DATE, (int) (d - 1));
    } else {
        c.add(Calendar.DATE, (int) d);
    }
    Date nDate = dt;
    int flag = 0;
    int nwd = nonWorkingDays(nDate, c.getTime(), NonWorkDays, CmpHoliDays);
    while (nwd != 0) {
        nDate = c.getTime();
        if (nwd == 1 && flag == 0) {
            c.add(Calendar.DATE, nwd);
            flag = 1;
        } else {
            c.add(Calendar.DATE, nwd - 1);
        }
        nwd = nonWorkingDays(nDate, c.getTime(), NonWorkDays, CmpHoliDays);
    }
    dt = c.getTime();
    edate = sdfLong.format(dt);
    if (Arrays.binarySearch(NonWorkDays, (c.get(Calendar.DAY_OF_WEEK) - 1)) > -1) {
        edate = getNextWorkingDay(edate, NonWorkDays, CmpHoliDays);
    }
    dt = sdfLong.parse(edate);
    return dt;
}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * ?? (1)// w  ww . java  2 s .  co  m
 *
 * @param date
 * @param amount ex) last month : -1. the current month : 0. the next month : 1
 * @return A date at the beginning of the month.
 */
public static Date getDateAtTheBeginningOfTheMonth(final Date date, final int amount) {
    final Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    cal.set(Calendar.DATE, 1);
    cal.add(Calendar.MONTH, amount);
    return cal.getTime();
}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * ?//from   w w w  .ja va2  s  .c o m
 *
 * @param date
 * @param amount ex) last month : -1. the current month : 0. the next month : 1
 * @return The date of the end of the month.
 */
public static Date getDateOfTheEndOfTheMonth(final Date date, final int amount) {
    final Calendar cal = Calendar.getInstance();
    cal.setTime(date);

    cal.set(Calendar.DATE, 1);
    cal.add(Calendar.MONTH, amount + 1);
    cal.add(Calendar.DATE, -1);
    return cal.getTime();
}

From source file:ether.RDFHelper.java

public static Model create(String jsonInput, String date) {
    JSONObject jso = new JSONObject(jsonInput);
    String today = "";
    String yesterday = "";
    String etherSchemaFileName = "ether-schema.ttl";
    Model model = ModelFactory.createDefaultModel();
    model.read(etherSchemaFileName, "TURTLE");

    try {/*from   ww w . jav a 2s  .  c om*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar temp = new GregorianCalendar();
        temp.setTime(sdf.parse(date));
        temp.add(Calendar.DAY_OF_MONTH, -1);
        yesterday = sdf.format(temp.getTime());
        today = date;
    } catch (Exception e) {
        e.printStackTrace();
    }

    String qualityToday = jso.getString("gentoday").trim();
    String qualityYesterday = jso.getString("genyest").trim();

    if (levelMap.containsKey(qualityToday)) {

        Resource todayEstimate = model.createResource();
        Resource rdfType = ResourceFactory
                .createResource("http://med.auth.gr/lomi/ether-schema#AirPollutionQualityEstimate");
        todayEstimate.addProperty(RDF.type, rdfType);

        Property estimateForNetwork = ResourceFactory
                .createProperty("http://med.auth.gr/lomi/ether-schema#estimateForNetwork");
        Resource network = ResourceFactory.createResource(
                "http://med.auth.gr/lomi/ether-schema#ThessalonikiMunicipalAirPollutionMeasurementNetwork");
        todayEstimate.addProperty(estimateForNetwork, network);

        Property measurementDate = ResourceFactory
                .createProperty("http://med.auth.gr/lomi/ether-schema#measurementDate");
        model.add(todayEstimate, measurementDate,
                ResourceFactory.createTypedLiteral(today, XSDDatatype.XSDdate));

        Property qualityEstimateValue = ResourceFactory
                .createProperty("http://med.auth.gr/lomi/ether-schema#qualityEstimateValue");
        Resource value = ResourceFactory
                .createResource("http://med.auth.gr/lomi/ether-schema#" + levelMap.get(qualityToday));
        todayEstimate.addProperty(qualityEstimateValue, value);
    }

    if (levelMap.containsKey(qualityYesterday)) {

        Resource yesterdayEstimate = model.createResource();
        Resource rdfType = ResourceFactory
                .createResource("http://med.auth.gr/lomi/ether-schema#AirPollutionQualityEstimate");
        yesterdayEstimate.addProperty(RDF.type, rdfType);

        Property estimateForNetwork = ResourceFactory
                .createProperty("http://med.auth.gr/lomi/ether-schema#estimateForNetwork");
        Resource network = ResourceFactory.createResource(
                "http://med.auth.gr/lomi/ether-schema#ThessalonikiMunicipalAirPollutionMeasurementNetwork");
        yesterdayEstimate.addProperty(estimateForNetwork, network);

        Property measurementDate = ResourceFactory
                .createProperty("http://med.auth.gr/lomi/ether-schema#measurementDate");
        model.add(yesterdayEstimate, measurementDate,
                ResourceFactory.createTypedLiteral(yesterday, XSDDatatype.XSDdate));

        Property qualityEstimateValue = ResourceFactory
                .createProperty("http://med.auth.gr/lomi/ether-schema#qualityEstimateValue");
        Resource value = ResourceFactory
                .createResource("http://med.auth.gr/lomi/ether-schema#" + levelMap.get(qualityYesterday));
        yesterdayEstimate.addProperty(qualityEstimateValue, value);
    }

    for (String keyP : pollutantDailyMap.keySet()) {
        for (String keyS : stationMap.keySet()) {
            String jsonKey = keyP + "_" + keyS;
            try {
                Double value = jso.getDouble(jsonKey);
                Resource dailyMeasurement = model.createResource();

                Resource rdfType = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#AverageDailyMeasurement");
                dailyMeasurement.addProperty(RDF.type, rdfType);

                Property measurementDate = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementDate");
                model.add(dailyMeasurement, measurementDate,
                        ResourceFactory.createTypedLiteral(yesterday, XSDDatatype.XSDdate));

                Property measurementFromStation = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementFromStation");
                Resource station = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#" + stationMap.get(keyS));
                dailyMeasurement.addProperty(measurementFromStation, station);

                Property measurementOfPollutant = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementOfPollutant");
                Resource pollutant = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#" + pollutantDailyMap.get(keyP));
                dailyMeasurement.addProperty(measurementOfPollutant, pollutant);

                Property measurementValue = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementValue");
                model.add(dailyMeasurement, measurementValue,
                        ResourceFactory.createTypedLiteral(value.toString(), XSDDatatype.XSDfloat));

                Property quantity = ResourceFactory.createProperty("http://qudt.org/schema/qudt#quantity");
                Resource density = ResourceFactory.createResource("http://qudt.org/vocab/quantity#Density");
                model.add(dailyMeasurement, quantity, density);

                Property unit = ResourceFactory.createProperty("http://qudt.org/schema/qudt#unit");
                Resource micro = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#MicrogramPerCubicMeter");
                Resource milli = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#MilligramPerCubicMeter");
                if (keyP.equals("co")) {
                    model.add(dailyMeasurement, unit, milli);
                } else {
                    model.add(dailyMeasurement, unit, micro);
                }
            } catch (Exception e) {
                //nothing
            }
        }
    }
    for (String keyP : pollutantHourlyMap.keySet()) {
        for (String keyS : stationMap.keySet()) {
            String jsonKey = keyP + "_" + keyS;
            try {
                Double value = jso.getDouble(jsonKey);
                Resource hourlyMeasurement = model.createResource();

                Resource rdfType = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#MaximumHourlyMeasurement");
                hourlyMeasurement.addProperty(RDF.type, rdfType);

                Property measurementDate = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementDate");
                model.add(hourlyMeasurement, measurementDate,
                        ResourceFactory.createTypedLiteral(today, XSDDatatype.XSDdate));

                Property measurementFromStation = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementFromStation");
                Resource station = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#" + stationMap.get(keyS));
                hourlyMeasurement.addProperty(measurementFromStation, station);

                Property measurementOfPollutant = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementOfPollutant");
                Resource pollutant = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#" + pollutantHourlyMap.get(keyP));
                hourlyMeasurement.addProperty(measurementOfPollutant, pollutant);

                Property measurementValue = ResourceFactory
                        .createProperty("http://med.auth.gr/lomi/ether-schema#measurementValue");
                model.add(hourlyMeasurement, measurementValue,
                        ResourceFactory.createTypedLiteral(value.toString(), XSDDatatype.XSDfloat));

                Property quantity = ResourceFactory.createProperty("http://qudt.org/schema/qudt#quantity");
                Resource density = ResourceFactory.createResource("http://qudt.org/vocab/quantity#Density");
                model.add(hourlyMeasurement, quantity, density);

                Property unit = ResourceFactory.createProperty("http://qudt.org/schema/qudt#unit");
                Resource micro = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#MicrogramPerCubicMeter");
                Resource milli = ResourceFactory
                        .createResource("http://med.auth.gr/lomi/ether-schema#MilligramPerCubicMeter");
                if (keyP.equals("co")) {
                    model.add(hourlyMeasurement, unit, milli);
                } else {
                    model.add(hourlyMeasurement, unit, micro);
                }
            } catch (Exception e) {
                //nothing
            }
        }
    }

    return model;
}

From source file:com.linuxbox.enkive.teststats.StatsHourGrainTest.java

@SuppressWarnings("unchecked")
@BeforeClass/*from   w  ww.  ja  v a 2  s  . co  m*/
public static void setUp() throws ParseException, GathererException {
    gatherTester = TestHelper.BuildGathererService();
    coll = TestHelper.GetTestCollection();
    client = TestHelper.BuildClient();
    grain = new HourConsolidator(client);

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.MILLISECOND, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    for (int i = 0; i < 10; i++) {
        List<RawStats> stats = gatherTester.gatherStats();
        List<Map<String, Object>> statsToStore = createListOfMaps();
        if (i == 5) {
            cal.add(Calendar.HOUR, -1);
        }

        for (RawStats data : stats) {
            Map<String, Object> temp = data.toMap();
            Map<String, Object> date = (Map<String, Object>) temp.get(STAT_TIMESTAMP);
            date.put(CONSOLIDATION_MIN, cal.getTime());
            date.put(CONSOLIDATION_MAX, cal.getTime());
            date.put(STAT_TS_POINT, cal.getTime());
            statsToStore.add(temp);
        }
        client.storeData(statsToStore);
    }
    dataCount = coll.count();
}

From source file:com.reizes.shiva.utils.CommonUtil.java

/**
 *  ?    ./*from  www .j  a  v a 2 s.  com*/
 */
public static Date getMinus1Day(Date date) {
    if (date == null) {
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, -1);
    return cal.getTime();
}

From source file:com.reizes.shiva.utils.CommonUtil.java

/**
 *  ?  ?  ./*from  w  w  w. j  a  v a  2s . c  o m*/
 */
public static Date getPlus1Day(Date date) {
    if (date == null) {
        return null;
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.add(Calendar.DATE, 1);
    return cal.getTime();
}

From source file:com.example.app.repository.model.RepositoryDAO.java

private static Date getNow() {
    final Calendar calendar = Calendar.getInstance(AppUtil.staticGetDefaultTimeZone(), Locale.ENGLISH);
    calendar.add(Calendar.HOUR_OF_DAY, -1);// truncate to an hour for caching
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    return calendar.getTime();
}

From source file:com.inmobi.messaging.consumer.util.TestUtil.java

static void publishLastPath(FileSystem fs, Path baseDir, Date lastCommitTime) throws IOException {
    if (lastCommitTime != null) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(lastCommitTime);/*from ww w  .  j a v  a  2s  .c  o m*/
        cal.add(Calendar.MINUTE, 1);
        Path minDir = DatabusStreamReader.getMinuteDirPath(baseDir, cal.getTime());
        fs.mkdirs(minDir);
        LOG.info("Created minDir:" + minDir);
    }
}

From source file:com.fengduo.bee.commons.util.DateViewTools.java

public static Date getDateBefore(int before) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_YEAR, -before);
    cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
    return cal.getTime();
}