java util Date Class - java.util.Date Class in Java
Date Class in Java
Java Date Class
Java Date class is used to handle Date and Time. Java Date class gets the current system date and time. And also create Date object with the given date and time.
Java Date class allowed the interpretation of dates as year, month, day, hour, minute, and second values. Java Date class also allowed the formatting and parsing of date strings. Unfortunately, the API for these functions was not amenable to internationalization. As of JDK 1.1, the Java Calendar class should be used to convert between dates and time fields and the DateFormat class should be used to format and parse date strings. The corresponding methods in Java Date class are deprecated.
All methods of Java Date class that accept or return year, month, date, hours, minutes, and seconds values, the following representations are used :
- A year y is represented by the integer y - 1900.
- A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.
- A date (day of month) is represented by an integer from 1 to 31 in the usual manner.
- An hour is represented by an integer from 0 to 23. Thus, the hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12.
- A minute is represented by an integer from 0 to 59 in the usual manner.
- A second is represented by an integer from 0 to 61; the values 60 and 61 occur only for leap seconds and even then only in Java implementations that actually track leap seconds correctly. Because of the manner in which leap seconds are currently introduced, it is extremely unlikely that two leap seconds will occur in the same minute, but this specification follows the date and time conventions for ISO C.
In all cases, arguments given to methods for these purposes need not fall within the indicated ranges; for example,
a date may be specified as January 32 and is interpreted as meaning February 1.
About Date and Time in Java
Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.
Some computer standards are defined in terms of Greenwich mean time (GMT), which is equivalent to universal time (UT). GMT is the "civil" name for the standard; UT is the "scientific" name for the same standard. The distinction between UTC and UT is that UTC is based on an atomic clock and UT is based on astronomical observations, which for all practical purposes is an invisibly fine hair to split. Because the earth's rotation is not uniform (it slows down and speeds up in complicated ways), UT does not always flow uniformly. Leap seconds are introduced as needed into UTC so as to keep UTC within 0.9 seconds of UT1, which is a version of UT with certain corrections applied. There are other time and date systems as well; for example, the time scale used by the satellite-based global positioning system (GPS) is synchronized to UTC but is not adjusted for leap seconds. An interesting source of further information is the United States Naval Observatory (USNO).
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.
java.util.Date Class Example
In the following Java Date example, you can learn how to set Date, Month, Year to Java Date Object.
How to set Date, Month and Year to Java Date Object
In the following Java Date example you can learn how to get GMT and Locale Date and Time String using Java Date class.
How to get GMT and Locale Date and Time String using Java Date
In the following example you can learn how to create Java Date object using parameter constructor.
How to Create Java Date object using Parameter Constructor