Example usage for java.util GregorianCalendar get

List of usage examples for java.util GregorianCalendar get

Introduction

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

Prototype

public int get(int field) 

Source Link

Document

Returns the value of the given calendar field.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    GregorianCalendar newCal = new GregorianCalendar();
    int day = newCal.get(Calendar.DAY_OF_WEEK);

    newCal = new GregorianCalendar();
    newCal.set(1997, 2, 1, 0, 0, 0);/*from ww  w  .j  a  va 2  s .  co  m*/
    newCal.setTime(newCal.getTime());
    day = newCal.get(Calendar.DAY_OF_WEEK);

}

From source file:MainClass.java

public static void main(String args[]) {
    GregorianCalendar gc = new GregorianCalendar();
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);//w w w  .j a  va2 s. c o m
    System.out.println(gc.isLeapYear(year));
    System.out.println("Month = " + gc.get(Calendar.MONTH));
    System.out.println("Week of year = " + gc.get(Calendar.WEEK_OF_YEAR));
    System.out.println("Week of month = " + gc.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Day of year = " + gc.get(Calendar.DAY_OF_YEAR));
    System.out.println("Day of week = " + gc.get(Calendar.DAY_OF_WEEK));
}

From source file:PriorityQueueTest.java

public static void main(String[] args) {
    PriorityQueue<GregorianCalendar> pq = new PriorityQueue<GregorianCalendar>();
    pq.add(new GregorianCalendar(1906, Calendar.DECEMBER, 9)); // G. Hopper
    pq.add(new GregorianCalendar(1815, Calendar.DECEMBER, 10)); // A. Lovelace
    pq.add(new GregorianCalendar(1903, Calendar.DECEMBER, 3)); // J. von Neumann
    pq.add(new GregorianCalendar(1910, Calendar.JUNE, 22)); // K. Zuse

    System.out.println("Iterating over elements...");
    for (GregorianCalendar date : pq)
        System.out.println(date.get(Calendar.YEAR));
    System.out.println("Removing elements...");
    while (!pq.isEmpty())
        System.out.println(pq.remove().get(Calendar.YEAR));
}

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 ww w. ja v a2s  .c  o m
}

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;/*w w w.j av a2  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) {
    GregorianCalendar gc = new GregorianCalendar();
    // current year value
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);//  ww w.jav a 2 s . c o m
    // current month value
    int month = gc.get(Calendar.MONTH);
    System.out.println(month);
    // day of month
    int day = gc.get(Calendar.DAY_OF_MONTH);
    System.out.println(day);
    // hour value
    int hour = gc.get(Calendar.HOUR);
    System.out.println(hour);
    // minute value
    int minute = gc.get(Calendar.MINUTE);
    System.out.println(minute);
    // second values
    int second = gc.get(Calendar.SECOND);
    System.out.println(second);
}

From source file:MainClass.java

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

    java.util.Date today = new java.util.Date();
    java.sql.Date date = new java.sql.Date(today.getTime());
    java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
    cal.setTime(date);//from w  w  w  .jav  a 2s . c o m
    System.out.println(cal.get(java.util.Calendar.DAY_OF_WEEK));

}

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    System.out.println(day);/*from   www.  ja  va  2s  .  c o m*/
}

From source file:MainClass.java

public static void main(String[] a) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.roll(Calendar.MONTH, false); // Go back a month
    System.out.println(calendar.get(Calendar.MONTH));
}

From source file:MainClass.java

public static void main(String[] args) {

    GregorianCalendar birthdate = new GregorianCalendar(1999, 1, 1);
    GregorianCalendar today = new GregorianCalendar(); // Today's date
    GregorianCalendar birthday = new GregorianCalendar(today.get(YEAR), birthdate.get(MONTH),
            birthdate.get(DATE));/*from   www .j  a  v a  2 s.c  o m*/
    int age = today.get(today.YEAR) - birthdate.get(YEAR);
    String[] weekdays = new DateFormatSymbols().getWeekdays(); // Get day names
    System.out.println("You were born on a " + weekdays[birthdate.get(DAY_OF_WEEK)]);
    System.out.println("This year you " + (birthday.after(today) ? " will be " : "are ") + age + " years old.");
    System.out
            .println("In " + today.get(YEAR) + " your birthday " + (today.before(birthday) ? "will be" : "was")
                    + " on a " + weekdays[birthday.get(DAY_OF_WEEK)] + ".");
}