Example usage for java.util Calendar get

List of usage examples for java.util Calendar get

Introduction

In this page you can find the example usage for java.util Calendar 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 {
    Calendar japanCal = new GregorianCalendar(TimeZone.getTimeZone("Japan"));
    japanCal.set(Calendar.HOUR_OF_DAY, 10); // 0..23
    japanCal.set(Calendar.MINUTE, 0);
    japanCal.set(Calendar.SECOND, 0);

    Calendar local = new GregorianCalendar();
    local.setTimeInMillis(japanCal.getTimeInMillis());
    int hour = local.get(Calendar.HOUR); // 5
    int minutes = local.get(Calendar.MINUTE); // 0
    int seconds = local.get(Calendar.SECOND); // 0
    boolean am = local.get(Calendar.AM_PM) == Calendar.AM; // false
}

From source file:KVMCalendar.java

public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    Date date = new Date();
    cal.setTime(date);// w ww  .  j  a  v a2s . c om
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    System.out.println("Day is " + day + ", month is " + month);

    final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
    long offset = date.getTime();
    offset += 20 * MILLIS_PER_DAY;
    date.setTime(offset);
    cal.setTime(date);

    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    System.out.println("In 20 days time, day will " + day + ", month will be " + month);
    System.out.println(cal);
}

From source file:DayWeek.java

public static void main(String[] av) {
    //+/* ww  w.ja  v  a2 s. c o  m*/
    Calendar c = Calendar.getInstance(); // today

    System.out.println("Year: " + c.get(Calendar.YEAR));
    System.out.println("Month: " + c.get(Calendar.MONTH));
    System.out.println("Day: " + c.get(Calendar.DAY_OF_MONTH));
    System.out.println("Day of week = " + c.get(Calendar.DAY_OF_WEEK));
    System.out.println("Day of year = " + c.get(Calendar.DAY_OF_YEAR));
    System.out.println("Week in Year: " + c.get(Calendar.WEEK_OF_YEAR));
    System.out.println("Week in Month: " + c.get(Calendar.WEEK_OF_MONTH));
    System.out.println("Day of Week in Month: " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH));
    System.out.println("Hour: " + c.get(Calendar.HOUR));
    System.out.println("AM or PM: " + c.get(Calendar.AM_PM));
    System.out.println("Hour (24-hour clock): " + c.get(Calendar.HOUR_OF_DAY));
    System.out.println("Minute: " + c.get(Calendar.MINUTE));
    System.out.println("Second: " + c.get(Calendar.SECOND));
    //-
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // display the current calendar
    System.out.println("Month is " + cal.get(Calendar.MONTH));

    // roll month
    cal.roll(Calendar.MONTH, 2);//from ww  w. ja  v a  2  s  .  c  o m

    // print result after rolling
    System.out.println("Month is " + cal.get(Calendar.MONTH));

    // roll downwards
    cal.roll(Calendar.MONTH, -4);

    // print result
    System.out.println("Month is " + cal.get(Calendar.MONTH));
}

From source file:Main.java

public static void main(String[] args) {
    int dayOfWeek = Calendar.SUNDAY;
    Calendar cal = new GregorianCalendar();
    cal.set(2015, 0, 1, 0, 0);/* w w w . ja  v  a2 s .c om*/
    cal.set(Calendar.DAY_OF_WEEK, dayOfWeek);
    while (cal.get(Calendar.YEAR) == 2015) {
        System.out.println(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, 7);
    }
}

From source file:Main.java

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

    int dayOfWeek = now.get(Calendar.DAY_OF_WEEK);
    if (Calendar.SUNDAY == dayOfWeek) {
        System.out.println("SUNDAY");
    }/*from   w w  w .j a  v  a 2  s.c  om*/
    if (Calendar.MONDAY == dayOfWeek) {
        System.out.println("MONDAY");
    }
    if (Calendar.TUESDAY == dayOfWeek) {
        System.out.println("TUESDAY");
    }
    if (Calendar.WEDNESDAY == dayOfWeek) {
        System.out.println("WEDNESDAY");
    }
    if (Calendar.THURSDAY == dayOfWeek) {
        System.out.println("THURSDAY");
    }
    if (Calendar.FRIDAY == dayOfWeek) {
        System.out.println("FRIDAY");
    }
    if (Calendar.SATURDAY == dayOfWeek) {
        System.out.println("SATURDAY");
    }
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // displays the current calendar
    System.out.println("Month is " + cal.get(Calendar.MONTH));

    // roll month
    cal.roll(Calendar.MONTH, true);

    // print result after rolling
    System.out.println("Month is " + cal.get(Calendar.MONTH));

    // roll downwards
    cal.roll(Calendar.MONTH, false);

    // print result after rolling down
    System.out.println("Month is " + cal.get(Calendar.MONTH));
}

From source file:Main.java

public static void main(String[] args) {

    Calendar cal = Calendar.getInstance();

    // get the value of all the calendar date fields.
    System.out.println("Calendar's Year: " + cal.get(Calendar.YEAR));
    System.out.println("Calendar's Month: " + cal.get(Calendar.MONTH));
    System.out.println("Calendar's Day: " + cal.get(Calendar.DATE));
}

From source file:MainClass.java

public static void main(String args[]) {
    Calendar cal = Calendar.getInstance();
    Date now = new Date();
    cal.setTime(now);/*from  ww  w  . j  a  v  a 2s.  co  m*/
    int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
    if (dayofweek == Calendar.SUNDAY)
        System.out.println("SUNDAY");
    if (dayofweek == Calendar.MONDAY)
        System.out.println("MONDAY");
    if (dayofweek == Calendar.TUESDAY)
        System.out.println("TUESDAY");
    if (dayofweek == Calendar.WEDNESDAY)
        System.out.println("WEDNESDAY");
    if (dayofweek == Calendar.THURSDAY)
        System.out.println("THURSDAY");
    if (dayofweek == Calendar.FRIDAY)
        System.out.println("FRIDAY");
    if (dayofweek == Calendar.SATURDAY) {
        System.out.println("SATURDAY");
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    String months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    Calendar calendar = Calendar.getInstance();

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

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

    calendar.set(Calendar.HOUR, 10);
    calendar.set(Calendar.MINUTE, 29);
    calendar.set(Calendar.SECOND, 22);

    System.out.print("Updated time: ");
    System.out.print(calendar.get(Calendar.HOUR) + ":");
    System.out.print(calendar.get(Calendar.MINUTE) + ":");
    System.out.println(calendar.get(Calendar.SECOND));
}