java util Calendar Class - java.util.Calendar Class in Java
Calendar Class in Java
The Java Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.
The class also provides additional fields and methods for implementing a concrete calendar system outside the package. Those fields and methods are defined as protected. Like other locale-sensitive classes, Java Calendar provides a class method, getInstance, for getting a generally useful object of this type. Java Calendar's getInstance method returns a Java Calendar object whose calendar fields have been initialized with the current date and time :
Calendar rightNow = Calendar.getInstance();
A Java Calendar object can produce all the calendar field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional). Java Calendar defines the range of values returned by certain calendar fields, as well as their meaning. For example, the first month of the calendar system has value MONTH == JANUARY for all calendars. Other values are defined by the concrete subclass, such as ERA.
How to Getting and Setting Calendar Field Values in Java ?
The calendar field values can be set by calling the set methods. Any field values set in a Java Calendar will not be interpreted until it needs to calculate its time value (milliseconds from the Epoch) or values of the calendar fields. Calling the get, getTimeInMillis, getTime, add and roll involves such calculation.
What is Leniency in Java Calendar ?
Java Calendar has two modes for interpreting the calendar fields, lenient and non-lenient. When a Java Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Java Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized.
For example, a lenient GregorianCalendar interprets
MONTH == JANUARY,
DAY_OF_MONTH == 32 as February 1.
When a Java Calendar is in non-lenient mode, it throws an exception if there is any inconsistency in its calendar fields. For example, a GregorianCalendar always produces DAY_OF_MONTH values between 1 and the length of the month. A non-lenient GregorianCalendar throws an exception upon calculating its time or calendar field values if any out-of-range field value has been set.
How to convert milliseconds to seconds with precision ?
There's no need to manually do the two parts separately - you just need floating point arithmetic, which the use of 1000.0 (as a double literal) forces. (I'm assuming your milliseconds value is an integer of some form.) Note that as usual with double, you may not be able to represent the result exactly. Consider using BigDecimal if you want to represent 100ms as 0.1 seconds exactly. (Given that it's a physical quantity, and the 100ms wouldn't be exact in the first place, a double is probably appropriate.)
Why do we say 52 weeks in a year ?
There are 52 complete weeks in a year. The year has 365 days, leaving one extra day. A leap year has 366 days, adding a second extra day. This makes 52 1/7 weeks in a normal year and 52 2/7 weeks in a leap year. If the 1st of Jan is on day 0 and there is no leap year, then there are 365 days in the year with the last week having only 2 days, this gives 52 complete weeks of 7 days and a 2 day week. If the year is a leap year then the last week has 3 days. There are 52 complete weeks and a remaining 2 or 3 days in the last incomplete week. Actually there are week number 1 to 53, where week 1 is always the first week that starts on a Sunday and week 53 contains the remaining days which may include the overlap into January of the next year.
Following Java Calendar class example you can learn how to create Java Calendar. And also learn how to use Java Calendar class.
How to create Java Calendar
Following Java Calendar class example you can learn how to get date and time from java.util.Calendar class with Calendar Fields.
How to get Date and Time from Java Calendar
Following Java Calendar class example you can learn how to compare two java.util.Calendar objects and how to get the Maximum value and Minimum value of particular Calendar Field.
How to compare two Java Calendar objects
Following Java Calendar class example you can learn how to set the values to java.util.Calendar Object.
How to set the values to java.util.Calendar Object