Example usage for java.util GregorianCalendar add

List of usage examples for java.util GregorianCalendar add

Introduction

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

Prototype

@Override
public void add(int field, int amount) 

Source Link

Document

Adds the specified (signed) amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    calendar.add(Calendar.YEAR, -14);
    System.out.println(calendar.get(Calendar.YEAR));
}

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    calendar.add(Calendar.YEAR, 14); // 14 years into the future
    System.out.println(calendar.get(Calendar.YEAR));
}

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();

    // add 2 months 
    cal.add((GregorianCalendar.MONTH), 2);

    // print the modified date and time
    System.out.println(cal.getTime());

    // add 2 years 
    cal.add((GregorianCalendar.YEAR), 2);

    // print the modified date and time
    System.out.println(cal.getTime());

}

From source file:Main.java

public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar();
    System.out.println("Current  Date: " + gc.getTime());
    // Add 1 year 
    gc.add(Calendar.YEAR, 1);
    System.out.println(gc.getTime());

    // Add 15 days 
    gc.add(Calendar.DATE, 15);//w ww.java2s  . c  o  m
    System.out.println(gc.getTime());
}

From source file:GetLeadChanges.java

public static void main(String[] args) {
    System.out.println("Executing Get Lead Changes");
    try {//from w  w  w  .ja va2  s  .  c o  m
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsGetLeadChanges request = new ParamsGetLeadChanges();

        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<Integer> batchSize = objectFactory.createParamsGetLeadActivityBatchSize(10);
        request.setBatchSize(batchSize);

        ArrayOfString activities = new ArrayOfString();
        activities.getStringItems().add("Visit Webpage");
        activities.getStringItems().add("Click Link");

        JAXBElement<ArrayOfString> activityFilter = objectFactory
                .createParamsGetLeadChangesActivityNameFilter(activities);
        request.setActivityNameFilter(activityFilter);

        // Create oldestCreateAt timestamp from 5 days ago
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(new Date().getTime());
        gc.add(GregorianCalendar.DAY_OF_YEAR, -5);

        DatatypeFactory factory = DatatypeFactory.newInstance();
        JAXBElement<XMLGregorianCalendar> oldestCreateAtValue = objectFactory
                .createStreamPositionOldestCreatedAt(factory.newXMLGregorianCalendar(gc));

        StreamPosition sp = new StreamPosition();
        sp.setOldestCreatedAt(oldestCreateAtValue);
        request.setStartPosition(sp);

        SuccessGetLeadChanges result = port.getLeadChanges(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLeadChanges.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    int year = 2015;
    int month = 4;

    SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");

    GregorianCalendar calendar = new GregorianCalendar(year, month, 1);

    while (calendar.get(GregorianCalendar.MONTH) == month) {
        String dateString = format.format(calendar.getTime());
        System.out.println(dateString);

        calendar.add(GregorianCalendar.DATE, 1);
    }//from  w  w w.j a v  a  2 s  . c  o  m
}

From source file:Main.java

public static void main(String[] args) {

    GregorianCalendar cal1 = (GregorianCalendar) GregorianCalendar.getInstance();

    // create a second calendar equal to first one
    GregorianCalendar cal2 = (GregorianCalendar) (Calendar) cal1.clone();

    // print cal2
    System.out.println(cal2.getTime());

    // compare the two calendars
    System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));

    // change cal 2 a bit
    cal2.add(GregorianCalendar.YEAR, 5);

    // compare the two calendars
    System.out.println("Cal1 and Cal2 are equal:" + cal1.equals(cal2));

}

From source file:org.dspace.checker.DailyReportEmailer.java

/**
 * Allows users to have email sent to them. The default is to send all
 * reports in one email//from   w  w w.j  a  v a 2  s.c om
 * 
 * @param args
 *            <dl>
 *            <dt>-h</dt>
 *            <dd>help</dd>
 *            <dt>-d</dt>
 *            <dd>Select deleted bitstreams</dd>
 *            <dt>-m</dt>
 *            <dd>Bitstreams missing from assetstore</dd>
 *            <dt>-c</dt>
 *            <dd>Bitstreams whose checksums were changed</dd>
 *            <dt>-n</dt>
 *            <dd>Bitstreams whose checksums were changed</dd>
 *            <dt>-a</dt>
 *            <dd>Send all reports in one email</dd>
 *            </dl>
 * 
 */
public static void main(String[] args) {
    // set up command line parser
    CommandLineParser parser = new PosixParser();
    CommandLine line = null;

    // create an options object and populate it
    Options options = new Options();

    options.addOption("h", "help", false, "Help");
    options.addOption("d", "Deleted", false, "Send E-mail report for all bitstreams set as deleted for today");
    options.addOption("m", "Missing", false,
            "Send E-mail report for all bitstreams not found in assetstore for today");
    options.addOption("c", "Changed", false,
            "Send E-mail report for all bitstreams where checksum has been changed for today");
    options.addOption("a", "All", false, "Send all E-mail reports");

    options.addOption("u", "Unchecked", false, "Send the Unchecked bitstream report");

    options.addOption("n", "Not Processed", false,
            "Send E-mail report for all bitstreams set to longer be processed for today");

    try {
        line = parser.parse(options, args);
    } catch (ParseException e) {
        log.fatal(e);
        System.exit(1);
    }

    // user asks for help
    if (line.hasOption('h')) {
        HelpFormatter myhelp = new HelpFormatter();

        myhelp.printHelp("Checksum Reporter\n", options);
        System.out.println("\nSend Deleted bitstream email report: DailyReportEmailer -d");
        System.out.println("\nSend Missing bitstreams email report: DailyReportEmailer -m");
        System.out.println("\nSend Checksum Changed email report: DailyReportEmailer -c");

        System.out.println("\nSend bitstream not to be processed email report: DailyReportEmailer -n");

        System.out.println("\nSend Un-checked bitstream report: DailyReportEmailer -u");

        System.out.println("\nSend All email reports: DailyReportEmailer");
        System.exit(0);
    }

    // create a new simple reporter
    SimpleReporter reporter = new SimpleReporterImpl();

    DailyReportEmailer emailer = new DailyReportEmailer();

    // get dates for yesterday and tomorrow
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.add(GregorianCalendar.DAY_OF_YEAR, -1);

    Date yesterday = calendar.getTime();
    calendar.add(GregorianCalendar.DAY_OF_YEAR, 2);

    Date tomorrow = calendar.getTime();

    File report = null;
    FileWriter writer = null;

    try {
        // the number of bitstreams in report
        int numBitstreams = 0;

        // create a temporary file in the log directory
        String dirLocation = ConfigurationManager.getProperty("log.dir");
        File directory = new File(dirLocation);

        if (directory.exists() && directory.isDirectory()) {
            report = File.createTempFile("checker_report", ".txt", directory);
        } else {
            throw new IllegalStateException("directory :" + dirLocation + " does not exist");
        }

        writer = new FileWriter(report);

        if ((line.hasOption("a")) || (line.getOptions().length == 0)) {
            writer.write("\n--------------------------------- Begin Reporting ------------------------\n\n");
            numBitstreams += reporter.getDeletedBitstreamReport(yesterday, tomorrow, writer);
            writer.write("\n--------------------------------- Report Spacer ---------------------------\n\n");
            numBitstreams += reporter.getChangedChecksumReport(yesterday, tomorrow, writer);
            writer.write("\n--------------------------------- Report Spacer ---------------------------\n\n");
            numBitstreams += reporter.getBitstreamNotFoundReport(yesterday, tomorrow, writer);
            writer.write("\n--------------------------------- Report Spacer ---------------------------\n\n");
            numBitstreams += reporter.getNotToBeProcessedReport(yesterday, tomorrow, writer);
            writer.write("\n--------------------------------- Report Spacer ---------------------------\n\n");
            numBitstreams += reporter.getUncheckedBitstreamsReport(writer);
            writer.write("\n--------------------------------- End Report ---------------------------\n\n");
            writer.flush();
            writer.close();
            emailer.sendReport(report, numBitstreams);
        } else {
            if (line.hasOption("d")) {
                writer.write(
                        "\n--------------------------------- Begin Reporting ------------------------\n\n");
                numBitstreams += reporter.getDeletedBitstreamReport(yesterday, tomorrow, writer);
                writer.flush();
                writer.close();
                emailer.sendReport(report, numBitstreams);
            }

            if (line.hasOption("m")) {
                writer.write(
                        "\n--------------------------------- Begin Reporting ------------------------\n\n");
                numBitstreams += reporter.getBitstreamNotFoundReport(yesterday, tomorrow, writer);
                writer.flush();
                writer.close();
                emailer.sendReport(report, numBitstreams);
            }

            if (line.hasOption("c")) {
                writer.write(
                        "\n--------------------------------- Begin Reporting ------------------------\n\n");
                numBitstreams += reporter.getChangedChecksumReport(yesterday, tomorrow, writer);
                writer.flush();
                writer.close();
                emailer.sendReport(report, numBitstreams);
            }

            if (line.hasOption("n")) {
                writer.write(
                        "\n--------------------------------- Begin Reporting ------------------------\n\n");
                numBitstreams += reporter.getNotToBeProcessedReport(yesterday, tomorrow, writer);
                writer.flush();
                writer.close();
                emailer.sendReport(report, numBitstreams);
            }

            if (line.hasOption("u")) {
                writer.write(
                        "\n--------------------------------- Begin Reporting ------------------------\n\n");
                numBitstreams += reporter.getUncheckedBitstreamsReport(writer);
                writer.flush();
                writer.close();
                emailer.sendReport(report, numBitstreams);
            }
        }
    } catch (MessagingException e) {
        log.fatal(e);
    } catch (IOException e) {
        log.fatal(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                log.fatal("Could not close writer", e);
            }
        }

        if (report != null && report.exists()) {
            if (!report.delete()) {
                log.error("Unable to delete report file");
            }
        }
    }
}

From source file:CalendarTest.java

public static void main(String[] args) {
    // construct d as current date
    GregorianCalendar d = new GregorianCalendar();

    int today = d.get(Calendar.DAY_OF_MONTH);
    int month = d.get(Calendar.MONTH);

    // set d to start date of the month
    d.set(Calendar.DAY_OF_MONTH, 1);

    int weekday = d.get(Calendar.DAY_OF_WEEK);

    // get first day of week (Sunday in the U.S.)
    int firstDayOfWeek = d.getFirstDayOfWeek();

    // determine the required indentation for the first line
    int indent = 0;
    while (weekday != firstDayOfWeek) {
        indent++;//from   www.  j  av  a 2s.  c o  m
        d.add(Calendar.DAY_OF_MONTH, -1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    }

    // print weekday names
    String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
    do {
        System.out.printf("%4s", weekdayNames[weekday]);
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);
    } while (weekday != firstDayOfWeek);
    System.out.println();

    for (int i = 1; i <= indent; i++)
        System.out.print("    ");

    d.set(Calendar.DAY_OF_MONTH, 1);
    do {
        // print day
        int day = d.get(Calendar.DAY_OF_MONTH);
        System.out.printf("%3d", day);

        // mark current day with *
        if (day == today)
            System.out.print("*");
        else
            System.out.print(" ");

        // advance d to the next day
        d.add(Calendar.DAY_OF_MONTH, 1);
        weekday = d.get(Calendar.DAY_OF_WEEK);

        // start a new line at the start of the week
        if (weekday == firstDayOfWeek)
            System.out.println();
    } while (d.get(Calendar.MONTH) == month);
    // the loop exits when d is day 1 of the next month

    // print final end of line if necessary
    if (weekday != firstDayOfWeek)
        System.out.println();
}

From source file:DateUtils.java

public static Date getYesterday() {
    GregorianCalendar yesterday = new GregorianCalendar();
    yesterday.add(Calendar.DATE, -1);
    return yesterday.getTime();
}