Get all attributes from a Calendar
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] a) {
Date timeNow = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(timeNow);
System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: "+ calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: "+ calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: "+ calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "+ (calendar.get(Calendar.ZONE_OFFSET) / (60 * 60 * 1000)));
System.out.println("DST_OFFSET: "+ (calendar.get(Calendar.DST_OFFSET) / (60 * 60 * 1000)));
}
}
Output:
ERA: 1
YEAR: 2012
MONTH: 8
WEEK_OF_YEAR: 36
WEEK_OF_MONTH: 2
DATE: 7
DAY_OF_MONTH: 7
DAY_OF_YEAR: 251
DAY_OF_WEEK: 6
DAY_OF_WEEK_IN_MONTH: 1
AM_PM: 0
HOUR: 10
HOUR_OF_DAY: 10
MINUTE: 7
SECOND: 23
MILLISECOND: 643
ZONE_OFFSET: -8
DST_OFFSET: 1
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Get:
- Day of Week
- Number of days in a month
- Month of year
- Current month name
- Full date time
- Get all attributes from a Calendar
- Get year, month, day, minute, hour, second, milli second from java.util.Date through Calendar
- Get age from a birthday date
- Get Monday
- Get next Sunday
- Get today's date
- Get the last day of a month
- Get date of yesterday
- Get Day-of-Week for a Particular Date
- Get date of last week
- Get date of last month
- Get last Date of This Month
- Get Month in a leap year
- Get start of a month
- Get the end of a month
- Get noon of a day
- Get days since a date
- Get TimeZone
- Get Last day fo previous Month
- Is an hour between an interval
- Is a Year a Leap Year