2. 39. 16. To obtain a date part, such as the hour, the month, or the year, use the get method |
|
public int get (int field)
|
|
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, and
- Calendar.MILLISECOND.
|
get(Calendar.MONTH) returns a zero-based index of the month, with 0 representing January, 1 February, and 11 December. |
import java.util.Calendar;
public class MainClass{
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));
}
}
|
|
2007
0
30
8
32
42
805 |