Java GregorianCalendar get field value

Introduction

To

The month value is from 0 to 11.

January is 0, February is 1, and so on.

import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
  public static void main(String[] args) {
    GregorianCalendar gc = new GregorianCalendar();
    // get year value
    int year = gc.get(Calendar.YEAR);
    System.out.println(year);/* w ww. ja  va  2s  .com*/
    // get month value
    int month = gc.get(Calendar.MONTH);
    System.out.println(month);
    // get day of month
    int day = gc.get(Calendar.DAY_OF_MONTH);
    System.out.println(day);
    // get hour value
    int hour = gc.get(Calendar.HOUR);
    System.out.println(hour);
    // get minute value
    int minute = gc.get(Calendar.MINUTE);
    System.out.println(minute);
    // get second values
    int second = gc.get(Calendar.SECOND);
    System.out.println(second);
  }

}



PreviousNext

Related