Example usage for java.util Calendar set

List of usage examples for java.util Calendar set

Introduction

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

Prototype

public void set(int field, int value) 

Source Link

Document

Sets the given calendar field to the given value.

Usage

From source file:com.discursive.jccook.lang.builders.reflect.PoliticalCandidate.java

public static void main(String[] args) {
    // START lang-ex-sample-1
    // Create a State
    State va = new State("VA", "Virginia");

    // Create a Birth Date
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.YEAR, 1743);
    calendar.set(Calendar.MONTH, Calendar.APRIL);
    calendar.set(Calendar.DAY_OF_MONTH, 13);
    Date dob = calendar.getTime();

    BigDecimal moneyRaised = new BigDecimal(293829292.93);

    // Create a Political Candidate
    PoliticalCandidate candidate = new PoliticalCandidate("Jefferson", "Thomas", dob, moneyRaised, va);

    System.out.println(candidate);
    // END lang-ex-sample-1
}

From source file:Main.java

public static void main(String[] args) {

    Date today = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(today);/*from   w  w w . j  a v  a 2  s .  com*/

    calendar.add(Calendar.MONTH, 1);
    calendar.set(Calendar.DAY_OF_MONTH, 1);
    calendar.add(Calendar.DATE, -1);

    Date lastDayOfMonth = calendar.getTime();

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println("Today            : " + sdf.format(today));
    System.out.println("Last Day of Month: " + sdf.format(lastDayOfMonth));
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();
    Calendar past = Calendar.getInstance();

    System.out.println("Current date: " + cal.getTime());

    // change year in past calendar
    past.set(Calendar.YEAR, 2013);
    System.out.println("Year is " + past.get(Calendar.YEAR));

    // check if calendar date is before current date
    System.out.println(cal.before(past));
}

From source file:Main.java

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

    calendar.set(Calendar.DAY_OF_MONTH, 1);
    // day of week for first date of month
    int weekOfFirstDate = calendar.get(Calendar.WEEK_OF_YEAR);

    int lastDateOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    calendar.set(Calendar.DAY_OF_MONTH, lastDateOfMonth);
    // day of week for last date of month

    int weekOfLastDate = calendar.get(Calendar.WEEK_OF_YEAR);

    calendar.roll(Calendar.MONTH, false);
    int lastDateOfPrevMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

    int weeksToDisplay = weekOfLastDate - weekOfFirstDate + 1;
    int[] days = new int[weeksToDisplay * 7];

    int firstDayPosition = 3;

    // fill previous month
    int x = lastDateOfPrevMonth;
    for (int i = firstDayPosition - 1; i >= 0; i--) {
        days[i] = x--;/*from w  w w  .j  a  v  a  2s  . c o m*/
    }

    // fill current month
    for (int i = 1; i < lastDateOfMonth + 1; i++) {
        days[firstDayPosition - 1 + i] = i;
    }

    // fill next month
    int j = 1;
    for (int i = lastDateOfMonth + firstDayPosition; i < days.length; i++) {
        days[i] = j++;
    }

    for (int i = 0; i < days.length; i++) {
        if (i % 7 == 0) {
            System.out.println();
        }
        System.out.print(days[i] + "\t");
    }
}

From source file:com.pureinfo.srm.config.notice.TestTimer.java

public static void main(String[] args) throws ParseException {
    Calendar start = new GregorianCalendar();
    start.setTime(format.parse("18:40"));
    Calendar cal = new GregorianCalendar();
    start.set(Calendar.YEAR, cal.get(Calendar.YEAR));
    start.set(Calendar.MONTH, cal.get(Calendar.MONTH));
    start.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
    while (start.before(cal)) {
        start.setTimeInMillis(start.getTimeInMillis() + DateUtils.MILLIS_PER_MINUTE);
    }/*  w w  w. ja va 2  s  . co  m*/
    new Timer().scheduleAtFixedRate(new test1(), start.getTime(), DateUtils.MILLIS_PER_MINUTE);
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // print current year
    System.out.println("Current year is :" + cal.get(Calendar.YEAR));

    // set the year to something else
    cal.set(Calendar.YEAR, 2013);

    // print the result
    System.out.println("Altered year is :" + cal.get(Calendar.YEAR));
}

From source file:GregCalDemo.java

public static void main(String[] av) {
    //+// w w  w  . j av  a2s .co m
    GregorianCalendar d1 = new GregorianCalendar(1986, 04, 05); // May 5
    GregorianCalendar d2 = new GregorianCalendar(); // today
    Calendar d3 = Calendar.getInstance(); // today

    System.out.println("It was then " + d1.getTime());
    System.out.println("It is now " + d2.getTime());
    System.out.println("It is now " + d3.getTime());
    d3.set(Calendar.YEAR, 1915);
    d3.set(Calendar.MONTH, Calendar.APRIL);
    d3.set(Calendar.DAY_OF_MONTH, 12);
    System.out.println("D3 set to " + d3.getTime());
    //-
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
    Date workDate = simpleDateFormat1.parse("2011-11-27");

    Calendar workCalendar = Calendar.getInstance();
    workCalendar.setTime(workDate);/* ww w.j  av  a 2 s .  co m*/
    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm:ss");
    Calendar time = Calendar.getInstance();
    time.setTime(simpleDateFormat2.parse("06:00:00"));
    workCalendar.set(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));
    workCalendar.set(Calendar.MINUTE, time.get(Calendar.MINUTE));
    workCalendar.set(Calendar.SECOND, time.get(Calendar.SECOND));

    Date newWorkDate = workCalendar.getTime();

    SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    System.out.println(simpleDateFormat3.format(newWorkDate));
}

From source file:com.appeligo.epg.EpgIndexer.java

public static void main(String[] args) throws Exception {
    HessianProxyFactory factory = new HessianProxyFactory();
    EPGProvider epg = (EPGProvider) factory.create(EPGProvider.class, "http://localhost/epg/channel.epg");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR, -5);//from  ww w . j a  va 2  s.c  o m
    cal.set(Calendar.DATE, 1);
    String[] lineups = new String[] { "SDTW-C", "P-C", "P-DC", "P-S", "M-C", "M-DC", "M-S", "E-C", "E-DC",
            "E-S", "H-C", "H-DC", "H-S" };
    List<String> ids = epg.getModifiedProgramIds(cal.getTime());
    int count = 0;
    long average = 0;
    int counter = 0;
    int added = 0;
    while (count < ids.size()) {
        System.err.println("in loop: " + counter + ", " + count + "," + ids.size());
        int subsetSize = (ids.size() < 100 ? ids.size() : 100);
        counter++;
        if (count % 1000 == 0) {
            log.debug("Index programs into the Lucene Index. Current have processed " + count
                    + " programs out of " + ids.size());
        }
        int endIndex = (count + subsetSize > ids.size() ? ids.size() : count + subsetSize);
        List<String> subset = ids.subList(count, endIndex);
        count += subsetSize;

        long time = System.currentTimeMillis();
        HashMap<String, List<ScheduledProgram>> schedules = new HashMap<String, List<ScheduledProgram>>();
        for (String lineup : lineups) {
            ScheduledProgram[] programs = epg.getNextShowingList(lineup, subset);
            for (ScheduledProgram program : programs) {
                if (program != null) {
                    List<ScheduledProgram> schedule = schedules.get(program.getProgramId());
                    if (schedule == null) {
                        schedule = new ArrayList<ScheduledProgram>();
                        schedules.put(program.getProgramId(), schedule);
                    }
                    schedule.add(program);
                    added++;
                }
            }
        }
        long after = System.currentTimeMillis();
        long diff = after - time;
        average += diff;
        System.err.println(diff + " - " + (average / counter) + " added: " + added);
    }
    //      EpgIndexer indexer = new EpgIndexer(programIndex, epg, lineup);
    //      Calendar cal = Calendar.getInstance();
    //      cal.set(Calendar.DAY_OF_MONTH, 24);
    //      cal.set(Calendar.HOUR_OF_DAY, 0);
    //      indexer.updateEpgIndex(cal.getTime());
}

From source file:search.java

public static void main(String argv[]) {
    int optind;/*from  w ww.  j a va  2 s .  c  o m*/

    String subject = null;
    String from = null;
    boolean or = false;
    boolean today = false;
    int size = -1;

    for (optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-T")) {
            protocol = argv[++optind];
        } else if (argv[optind].equals("-H")) {
            host = argv[++optind];
        } else if (argv[optind].equals("-U")) {
            user = argv[++optind];
        } else if (argv[optind].equals("-P")) {
            password = argv[++optind];
        } else if (argv[optind].equals("-or")) {
            or = true;
        } else if (argv[optind].equals("-D")) {
            debug = true;
        } else if (argv[optind].equals("-f")) {
            mbox = argv[++optind];
        } else if (argv[optind].equals("-L")) {
            url = argv[++optind];
        } else if (argv[optind].equals("-subject")) {
            subject = argv[++optind];
        } else if (argv[optind].equals("-from")) {
            from = argv[++optind];
        } else if (argv[optind].equals("-today")) {
            today = true;
        } else if (argv[optind].equals("-size")) {
            size = Integer.parseInt(argv[++optind]);
        } else if (argv[optind].equals("--")) {
            optind++;
            break;
        } else if (argv[optind].startsWith("-")) {
            System.out.println("Usage: search [-D] [-L url] [-T protocol] [-H host] "
                    + "[-U user] [-P password] [-f mailbox] "
                    + "[-subject subject] [-from from] [-or] [-today]");
            System.exit(1);
        } else {
            break;
        }
    }

    try {

        if ((subject == null) && (from == null) && !today && size < 0) {
            System.out.println("Specify either -subject, -from, -today, or -size");
            System.exit(1);
        }

        // Get a Properties object
        Properties props = System.getProperties();

        // Get a Session object
        Session session = Session.getInstance(props, null);
        session.setDebug(debug);

        // Get a Store object
        Store store = null;
        if (url != null) {
            URLName urln = new URLName(url);
            store = session.getStore(urln);
            store.connect();
        } else {
            if (protocol != null)
                store = session.getStore(protocol);
            else
                store = session.getStore();

            // Connect
            if (host != null || user != null || password != null)
                store.connect(host, user, password);
            else
                store.connect();
        }

        // Open the Folder

        Folder folder = store.getDefaultFolder();
        if (folder == null) {
            System.out.println("Cant find default namespace");
            System.exit(1);
        }

        folder = folder.getFolder(mbox);
        if (folder == null) {
            System.out.println("Invalid folder");
            System.exit(1);
        }

        folder.open(Folder.READ_ONLY);
        SearchTerm term = null;

        if (subject != null)
            term = new SubjectTerm(subject);
        if (from != null) {
            FromStringTerm fromTerm = new FromStringTerm(from);
            if (term != null) {
                if (or)
                    term = new OrTerm(term, fromTerm);
                else
                    term = new AndTerm(term, fromTerm);
            } else
                term = fromTerm;
        }
        if (today) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.HOUR, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.AM_PM, Calendar.AM);
            ReceivedDateTerm startDateTerm = new ReceivedDateTerm(ComparisonTerm.GE, c.getTime());
            c.add(Calendar.DATE, 1); // next day
            ReceivedDateTerm endDateTerm = new ReceivedDateTerm(ComparisonTerm.LT, c.getTime());
            SearchTerm dateTerm = new AndTerm(startDateTerm, endDateTerm);
            if (term != null) {
                if (or)
                    term = new OrTerm(term, dateTerm);
                else
                    term = new AndTerm(term, dateTerm);
            } else
                term = dateTerm;
        }

        if (size >= 0) {
            SizeTerm sizeTerm = new SizeTerm(ComparisonTerm.GT, size);
            if (term != null) {
                if (or)
                    term = new OrTerm(term, sizeTerm);
                else
                    term = new AndTerm(term, sizeTerm);
            } else
                term = sizeTerm;
        }

        Message[] msgs = folder.search(term);
        System.out.println("FOUND " + msgs.length + " MESSAGES");
        if (msgs.length == 0) // no match
            System.exit(1);

        // Use a suitable FetchProfile
        FetchProfile fp = new FetchProfile();
        fp.add(FetchProfile.Item.ENVELOPE);
        folder.fetch(msgs, fp);

        for (int i = 0; i < msgs.length; i++) {
            System.out.println("--------------------------");
            System.out.println("MESSAGE #" + (i + 1) + ":");
            dumpPart(msgs[i]);
        }

        folder.close(false);
        store.close();
    } catch (Exception ex) {
        System.out.println("Oops, got exception! " + ex.getMessage());
        ex.printStackTrace();
    }

    System.exit(1);
}