Example usage for java.util Calendar YEAR

List of usage examples for java.util Calendar YEAR

Introduction

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

Prototype

int YEAR

To view the source code for java.util Calendar YEAR.

Click Source Link

Document

Field number for get and set indicating the year.

Usage

From source file:MainClass.java

public static void main(String args[]) {

    Calendar calendar1 = Calendar.getInstance();
    int doy1 = calendar1.get(Calendar.DAY_OF_YEAR);

    int year = calendar1.get(Calendar.YEAR);

    Calendar calendar2 = new GregorianCalendar(year, 11, 31);
    int doy2 = calendar2.get(Calendar.DAY_OF_YEAR);

    int days = doy2 - doy1;
    System.out.println(days + " days remain in current year");
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // display the current date and time
    System.out.println("Current Calendar Date: " + cal.getTime());

    // use clear method to set year as undefined.
    cal.clear(Calendar.YEAR);

    // print the result
    System.out.println("The calendar shows : " + cal.getTime());

    // use clear method to set month as undefined.
    cal.clear(Calendar.MONTH);//from w w w  . j  a  v a  2 s.c o m

    System.out.println("The calendar shows : " + cal.getTime());
}

From source file:Main.java

public static void main(String[] args) {

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:00Z");
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, 2015);
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DATE, 1);//from w ww. j a v a2s.c om
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 15);
    Date date = cal.getTime();
    System.out.println(date);
    TimeZone tz = TimeZone.getTimeZone("IST");
    dateFormat.setTimeZone(tz);
    String actual = dateFormat.format(date);

    System.out.println(actual);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Calendar start = Calendar.getInstance();
    start.set(2015, 1, 21, 11, 1, 11);//from  ww w  . j a  va 2s. c om
    Calendar end = Calendar.getInstance();

    Calendar clone = (Calendar) start.clone();
    int year = elapsed(clone, end, Calendar.YEAR);
    System.out.println(year + " year");
    clone.add(Calendar.YEAR, year);
    int month = elapsed(clone, end, Calendar.MONTH);
    clone.add(Calendar.MONTH, month);
    System.out.println(month + " month");

    int date = elapsed(clone, end, Calendar.DATE);
    clone.add(Calendar.DATE, date);
    System.out.println(date + " day");

    int hour = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 3600000;
    clone.add(Calendar.HOUR, hour);
    System.out.println(hour + " hour");

    int minute = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 60000;
    clone.add(Calendar.MINUTE, minute);
    System.out.println(minute + " minute");

    int millis = (int) (end.getTimeInMillis() - clone.getTimeInMillis()) / 1000;
    System.out.println(millis + " millis");

}

From source file:MainClass.java

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

    int todayMonth = today.get(Calendar.MONTH);

    int todayDayOfMonth = today.get(Calendar.DAY_OF_MONTH);

    int todayYear = today.get(Calendar.YEAR);

    int todayDayOfYear = today.get(Calendar.DAY_OF_YEAR);

    int numDays;//  ww w .j a  v a  2 s. c o  m
    if (todayMonth == 11 && todayDayOfMonth > 25) {
        GregorianCalendar xmas = new GregorianCalendar(todayYear + 1, 11, 25);
        int xmasDayOfYear = xmas.get(Calendar.DAY_OF_YEAR);
        numDays = xmasDayOfYear + 31 - todayDayOfMonth;
    } else {
        GregorianCalendar xmas = new GregorianCalendar(todayYear, 11, 25);
        int xmasDayOfYear = xmas.get(Calendar.DAY_OF_YEAR);
        numDays = xmasDayOfYear - todayDayOfYear;
    }

    System.out.println("The number of days till Christmas is " + numDays);
}

From source file:Main.java

public static void main(String[] args) throws ParseException {

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

    String oeStartDateStr = "04/01/2015";
    String oeEndDateStr = "11/14/2015";

    Calendar cal = Calendar.getInstance();
    Integer year = cal.get(Calendar.YEAR);

    oeStartDateStr = oeStartDateStr.concat(year.toString());
    oeEndDateStr = oeEndDateStr.concat(year.toString());

    Date startDate = sdf.parse(oeStartDateStr);
    Date endDate = sdf.parse(oeEndDateStr);
    Date d = new Date();
    String currDt = sdf.format(d);

    if ((d.after(startDate) && (d.before(endDate)))
            || (currDt.equals(sdf.format(startDate)) || currDt.equals(sdf.format(endDate)))) {
        System.out.println("Date is between 1st april to 14th nov...");
    } else {/*from ww  w . j  a  v  a  2  s. c o  m*/
        System.out.println("Date is not between 1st april to 14th nov...");
    }
}

From source file:DateCalAdd.java

public static void main(String[] av) {
    /** Today's date */
    Calendar now = Calendar.getInstance();

    /* Do "DateFormat" using "simple" format. */
    SimpleDateFormat formatter = new SimpleDateFormat("E yyyy/MM/dd 'at' hh:mm:ss a zzz");
    System.out.println("It is now " + formatter.format(now.getTime()));

    now.add(Calendar.YEAR, -2);
    System.out.println("Two years ago was " + formatter.format(now.getTime()));
}

From source file:Main.java

public static void main(String[] args) {
    Calendar now = Calendar.getInstance();

    System.out.println("Current week of month is : " + now.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Current week of year is : " + now.get(Calendar.WEEK_OF_YEAR));

    now.add(Calendar.WEEK_OF_MONTH, 1);

    System.out.println("date after one year : " + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
            + "-" + now.get(Calendar.YEAR));
}

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();//from w  w  w .  j  a v a2 s.c o  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);
}

From source file:GregorianCalendarDemo.java

public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    int year;/*w  ww. jav a 2s  .  c om*/

    GregorianCalendar gcalendar = new GregorianCalendar();

    System.out.print("Date: ");
    System.out.print(months[gcalendar.get(Calendar.MONTH)]);
    System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
    System.out.println(year = gcalendar.get(Calendar.YEAR));

    System.out.print("Time: ");
    System.out.print(gcalendar.get(Calendar.HOUR) + ":");
    System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
    System.out.println(gcalendar.get(Calendar.SECOND));

    if (gcalendar.isLeapYear(year)) {
        System.out.println("The current year is a leap year");
    } else {
        System.out.println("The current year is not a leap year");
    }
}