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:Main.java

public static void main(String args[]) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal2.add(Calendar.HOUR, 2);
    cal2.add(Calendar.MINUTE, 42);
    cal2.add(Calendar.SECOND, 12);
    long secs = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / 1000;
    String display = String.format("%02d:%02d:%02d", secs / 3600, (secs % 3600) / 60, (secs % 60));
    System.out.println(display);/* w ww  .  j  a  va2  s.c  o  m*/

}

From source file:Main.java

public static void main(String[] args) {
    Date today = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, 1);
    Date tomorrow = calendar.getTime();

    if (tomorrow.after(today)) {
        System.out.println(tomorrow + " is after " + today);
    }/*from   w  ww  .  j a v  a2 s. c  om*/
}

From source file:Main.java

public static void main(String[] args) {
    Date today = new Date();

    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1);
    Date yesterday = calendar.getTime();

    if (yesterday.before(today)) {
        System.out.println(yesterday + " is before " + today);
    }/*from  w  w  w  . java 2  s.c  o m*/
}

From source file:Main.java

public static void main(String[] args) {

    Calendar today = new GregorianCalendar();
    Calendar purchased = new GregorianCalendar();

    purchased.add(Calendar.DAY_OF_MONTH, 1);

    Calendar lastActDate = new GregorianCalendar();
    lastActDate.setTimeInMillis(today.getTimeInMillis() - purchased.getTimeInMillis());
}

From source file:Main.java

public static void main(String[] args) {
    Date d1 = new Date();
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 22);
    Date d2 = cal.getTime();//from   www .ja  v  a  2  s  . c o  m

    Iterator<Date> i = new DateIterator(d1, d2);
    while (i.hasNext()) {
        Date date = i.next();
        System.out.println(date);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    Calendar now = Calendar.getInstance();
    Calendar earliest = (Calendar) now.clone();
    earliest.add(Calendar.MONTH, -6);
    Calendar latest = (Calendar) now.clone();
    latest.add(Calendar.MONTH, 6);
    SpinnerModel model = new SpinnerDateModel(now.getTime(), earliest.getTime(), latest.getTime(),
            Calendar.WEEK_OF_YEAR);
    final JSpinner spinner = new JSpinner(model);

    model.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println(((SpinnerDateModel) e.getSource()).getDate());
        }//from   ww w .ja  v  a  2s  .  c om
    });

    frame.getContentPane().add("North", new JLabel("Choose a week"));
    frame.getContentPane().add("Center", spinner);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();
    // add seconds to current date
    now.add(Calendar.SECOND, 500);
    System.out.println("adding 500 seconds : ");
    System.out.println(now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));

    // adding minus value
    now = Calendar.getInstance();
    now.add(Calendar.SECOND, -500);
    System.out.println("adding -500 seconds: ");
    System.out.println(now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
}

From source file:Main.java

public static void main(String... args) {

    SimpleDateFormat df = new SimpleDateFormat("MM/dd/YYYY");

    Calendar c = Calendar.getInstance();
    System.out.println(df.format(c.getTime()));

    c.add(Calendar.YEAR, 60);
    System.out.println(df.format(c.getTime()));
}

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 av  a  2  s.  co  m

    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:SpinnerDateStartEndSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JSpinner Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Calendar cal = Calendar.getInstance();
    Date now = cal.getTime();/*  www.  j  av  a  2 s.  co  m*/
    cal.add(Calendar.YEAR, -50);
    Date startDate = cal.getTime();
    cal.add(Calendar.YEAR, 100);
    Date endDate = cal.getTime();
    SpinnerModel model = new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);

    JSpinner spinner1 = new JSpinner(model);

    JPanel panel1 = new JPanel(new BorderLayout());
    panel1.add(spinner1, BorderLayout.CENTER);
    frame.add(panel1, BorderLayout.SOUTH);

    frame.setSize(200, 90);
    frame.setVisible(true);
}