Get a date part, such as the hour, the month, or the year, use the get method in Java

Description

The following code shows how to get a date part, such as the hour, the month, or the year, use the get method.

To use it, pass a valid field to the get method. A valid field is one of the following values:

  • Calendar.YEAR
  • Calendar.MONTH
  • Calendar.DATE
  • Calendar.HOUR
  • Calendar.MINUTE
  • Calendar.SECOND
  • Calendar.MILLISECOND

get(Calendar.MONTH) returns a zero-based index of the month, with 0 representing January, 1 February, and 11 December.

Example


//from  ww w . j a va 2  s  .c om
  
import java.util.Calendar;

public class Main{

  public static void main(String[] args){
     Calendar calendar = Calendar.getInstance ();
     
     System.out.println(calendar.get(Calendar.YEAR));
     System.out.println(calendar.get(Calendar.MONTH));
     System.out.println(calendar.get(Calendar.DATE));
     System.out.println(calendar.get(Calendar.HOUR));
     System.out.println(calendar.get(Calendar.MINUTE));
     System.out.println(calendar.get(Calendar.SECOND));
     System.out.println(calendar.get(Calendar.MILLISECOND));
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    Date »




Date Get
Date Set
Date Format
Date Compare
Date Convert
Date Calculation
Date Parse
Timezone