Class Date in Java
Class Date is intended to reflect UTC(Universal Time Coordinate) time, but it may not do so exactly as it depends on the host environment of the java virtual machine rather it works as a system-independent way. We can create a Date by specifying the number of milliseconds from epoch( midnight GMT, January 1st, 1970).
The Date class encapsulates a point in time with millisecond precision. The value of a Date is represented internally by a long value that contains the number of milliseconds since midnight, January 1, 1970, GMT.
Prior to JDK 1.1, the Date class was used for two purposes that are now encapsulated by other classes. First, the Date class included methods for calculating calendar values, like months and days of the week. This functionality is now embedded in the Calendar class. Second, the Date class included methods for generating and parsing a string representation of a date. This functionality is now provided by java.text.DateFormat. Thus, as of JDK 1.1, most of the methods of Date are deprecated; the class is used only to represent a point in time.
The accurate measurement of time is a subject of considerable complexity and multifarious acronyms. There are two main methods of measuring time, atomic and astronomical. The U.S. Naval Observatory (http://tycho.usno.navy.mil) maintains a set of atomic clocks that provide the basis for Coordinated Universal Time (UTC). These clocks adhere to precise definitions of the second based on atomic decay.
Outside of the U.S. Navy, people tend to measure time in terms of Greenwich Mean Time (GMT). In the scientific community, GMT is called UT, which is a system of time predicated on the assumption that each rotation of the earth is exactly 24 * 60 * 60 seconds long.
Because the earth’s rotation is gradually slowing down, the seconds in UT are a little bit longer than the seconds in UTC. Now and then a “leap second” is added in UTC to keep it close to UT. Because the Date class simply measures milliseconds since a point in time, without regard for leap seconds, it is a good representation of UT or GMT.
All modern operating system assumes that one day =24 hours =24*60*60 seconds=86400 seconds in all Date related cases. In UTC, however, about once every year or two there is an extra second called ‘leap second’.The leap second is always added as the last second of the day and always on December 31st or June 30th. Most computer clocks are not accurate enough to be able to keep the second distinction.
The class Date provides an abstraction of dates and times. Dates may be constructed from the following six components…
- Year
- Month
- Date(day of the month)
- Hour
- Minute
- Seconds.
Also, the day of the week can be extracted from the date. Dates may be also compared and converted to readable String form. A date is represented to a precision of one millisecond. Some computer standards are defined in terms of GMT(Greenwich Mean Time) which is equivalent to UT-Universal Time. GMT is the civil name for the standard.UT is the scientific name for the same standard. UTC is based on an atomic clock and UT is based on an astronomical observation which is for all practical purposes. They both do not represent the same time. It is because the earth’s rotation is not uniform, it slows down and speeds up in a complicated way. As a result, UT does not flow uniformly. Leap seconds are introduced as needed into UTC to keep UTC up to date. One second of UT1, which is another version of UT with a certain correction applied is also available. There are several other time and date system as well.
The time scale used by GPS(satellite-based Global Positioning System) is synchronized to UTC but not adjusted for leap seconds.
In date, the below component exists
The year is represented by integer like y-2018
A month is represented by an integer from 0 to 11.0 being January and 11 being December.
Date(day of the month) is represented by an integer from 1 to 31
Hours are represented by an integer from 0 to 23. Thus hours from midnight 1 AM are hour 0 and the hours from noon 1 PM are hour 12.
A minute is represented by an integer from 0 to 59.
The second is represented by an integer from 0 to 60. The value 60 occurs only for Leap seconds and even then only java implementation that actually tracks leap seconds correctly.
The class structure of Date is as follows:
public class java.util.Date extends java.lang.Object implements java.io.Serializable,java.lang.Cloneable{
//constructors
public Date();
//allocates a Date Object and initializes it,so that it represents the time at which it was allocated
//measures to the nearest millisecond.
public Date(int year,int month,int date);
//allocates a Date Object and initializes it,so that it represents midnight,local time at the beginning
//of the day specified by the year,month and date arguments.
public Date(int year,int month,int date,int hrs,int min);
//allocates a Date Object and initializes it,so that it represents midnight,local time at the beginning
//of the day specified by the year,month,date,hrs and min arguments.
public Date(int year,int month,int date,int hrs,int min,int sec);
//allocates a Date Object and initializes it,so that it represents midnight,local time at the beginning
//of the day specified by the year,month,date,hrs,min an sec arguments.
public Date(long Date);
//allocates a Date object and initializes it to represent the specified number of milliseconds since
//January 1,1970,00:00:00 GMT
public Date(String s);
//allocates a Date object and initializes it to represent the date and time indicating by String s which is
//interpreted as if by the parse method.
//Method
public boolean after(Date when);
public boolean before(Date when);
public boolean equals(Object obj);
public int getDate();
public int getDay();
public int getHours();
public int getMinutes();
public int getSeconds();
public int getTimezoneOffset();
public int getYear();
public int getMonth();
public int hashCode();
public long getTime();
public static long parse(String s);
public static long UTC(int year,int month,int date,int hrs,int min,int sec);
public String toString();
public String toLocaleString();
public String toGMTString();
public void setDate(int date);
public void setHours(int hours);
public void setMinutes(int minutes);
public void setMonth(int month);
public void setSeconds(int seconds);
public void setTime(long time);
public void setYear(int year);
}
The details of the class structure are given as follows:
public Date();
public Date() constructor creates a Date object that is initialized to the current time.
public Date(long date);
public Date(long date) constructor creates a Date object that represents the given time.
Parameter
date – A time value, measured as the number of milliseconds since midnight, January 1, 1970 GMT.
public Date(int year, int month, int date);
public Date(int year, int month, int date); constructor creates a Date that represents midnight local time on the specified date.
Parameter
year – The year specified as a value that is added to 1900 to get the actual year.
month – The month specified in the range 0 to 11.
day – The day of the month specified in the range 1 to 31.
public Date(int year, int month, int date, int hrs, int min);
public Date(int year, int month, int date, int hrs, int min); constructor creates a Date that represents the given date and time.
Parameter
year – The year specified as a value that is added to 1900 to get the actual year.
month – The month specified in the range 0 to 11.
day – The day of the month specified in the range 1 to 31.
hrs – The hours specified in the range 0 to 23.
min – The minutes specified in the range 0 to 59.
public Date(int year, int month, int date, int hrs, int min, int sec);
public Date(int year, int month, int date, int hrs, int min, int sec); constructor creates a Date that represents the given date and time.
Parameter
year – The year specified as a value that is added to 1900 to get the actual year.
month – The month specified in the range 0 to 11.
day – The day of the month specified in the range 1 to 31.
hrs – The hours specified in the range 0 to 23.
min – The minutes specified in the range 0 to 59.
sec – The seconds specified in the range 0 to 59.
public Date(String s);
public Date(String s); constructor creates a Date that represents the date and time specified by the given string. The syntax of the date in the string must satisfy the requirements of the parse() method. The following is an example of a string that this constructor can understand:
Sat, 8 Feb 2019 13:30:00 GMT
Parameter
s – The string to parse.
public static long parse(String s);
public static long parse(String s); method returns the raw time value specified by the given string. This method understands a number of different formats. The following are examples of strings that this method can understand:
Sat, 8 Feb 2019 13:30:00 GMT
4/6/19
4/6/2019
January 5, 2019
2/4/19 11:03 AM
2/4/19 10:25 PM
2/4/19 17:03 GMT-6
2/4/19 17:03:24
March 16, 19 17:03 EST
March (comment)16, 19 (comment) 17:03 EST
16 march 2018 17:03 PDT
Sat 16 March 19 17:03 CST
The JDK 1.0.2 implementation of parse() has a serious bug. It incorrectly interprets date formats that specify the month as a number by making the month one greater than it should be. So 2/4/19 is incorrectly interpreted as March 4, 2019.
For the purposes of this method, UTC and GMT are considered equivalent.
This method returns a time value represented as the number of milliseconds since midnight, January 1, 1970, GMT.
Parameter
s – The string to parse.
public static long UTC(int year, int month, int date, int hrs, int min, int sec);
public static long UTC(int year, int month, int date, int hrs, int min, int sec); method returns a raw time value that corresponds to the given parameters. Computations are based on GMT, not the local time zone.
This method returns a time value represented as the number of milliseconds since midnight, January 1, 1970, GMT.
Parameter
year – The year specified as a value that is added to 1900 to get the actual year.
month – The month specified in the range 0 to 11.
day – The day of the month specified in the range 1 to 31.
hrs – The hours specified in the range 0 to 23.
min – The minutes specified in the range 0 to 59.
sec – The seconds specified in the range 0 to 59.
public boolean after(Date when);
public boolean after(Date when) method returns true if the value of when falls before the value of this Date.
This method returns true if this object is after when; false otherwise.
Parameter
when – The object to compare to this Date.
public boolean before(Date when);
public boolean before(Date when) method returns true if the value of when falls after the value of this Date.
This method returns true if this object is before when; false otherwise.
Parameter
when – The object to compare to this Date.
public boolean equals(Object obj);
public boolean equals(Object obj) method returns true if when is an instance of Date and it contains the same value as the object this method is associated with. In other words, the two Date objects are equal only if they both represent the same point in time, to the millisecond.
This method returns true if the objects are equal; false if they are not.
Parameter
obj – The object to be compared with this object.
public int getDate();
public int getDate() method returns the day of the month represented by this Date object. The value is in the range 1 to 31.
This method returns the day of the month of this Date.
public int getDay();
public int getDay(); method returns the day of the week represented by this Date object. The value is in the range 0 to 6, where 0 means Sunday.
This method returns the day of the week of this Date.
public int getHours();
public int getHours(); method returns the hour represented by this Date object. The value is in the range 0 to 23, where 0 means midnight.
This method returns the hour value of this Date.
public int getMinutes();
public int getMinutes(); method returns the number of minutes after the hour represented by this Date object. The value is in the range 0 to 59.
This method returns the minute value of this Date.
public int getMonth();
public int getMonth(); method returns the month represented by this Date object. The value is in the range 0 to 11, where 0 means January.
This method returns the month of this Date.
public int getSeconds();
public int getSeconds(); method returns the number of seconds after the minute represented by this Date object. The value is in the range 0 to 59.
This method returns the second value of this Date.
public long getTime();
public long getTime() method returns the date and time of this Date as the number of milliseconds since midnight, January 1, 1970 GMT.
This method returns the raw time value of this Date.
public int getTimezoneOffset();
public int getTimezoneOffset(); method returns the number of minutes between the local time zone and GMT for this Date object.
This method returns the time zone offset for this Date.
public int getYear();
public int getYear(); method returns the year represented by this Date object. The value is the number of years since 1990.
This method returns the year of this Date.
public int hashCode();
public int hashCode() method returns a hashcode for this object.
This method returns the hashcode for this Date.
public void setDate(int date);
public void setDate(int date); method sets the day of the month of this Date object.
Parameter
date – The day of the month specified in the range 1 to 31.
public void setHours(int hours);
public void setHours(int hours); method sets the hour of this Date object.
Parameter
hours – The hours specified in the range 0 to 23.
public void setMinutes(int minutes);
public void setMinutes(int minutes); method sets the minute value of this Date object.
Parameter
minutes – The minutes specified in the range 0 to 59.
public void setMonth(int month);
public void setMonth(int month); method sets the month of this Date object
Parameter
month – The month specified in the range 0 to 11.
public void setSeconds(int seconds);
public void setSeconds(int seconds); method sets the second value of this Date object.
Parameter
seconds – The seconds specified in the range 0 to 59.
public void setTime(long time);
public void setTime(long time) method sets the date and time represented by this Date to the given raw time value.
Parameter
time – A time value specified as the number of milliseconds since midnight, January 1, 1970 GMT.
public void setYear(int year);
public void setYear(int year); method sets the year of this Date object.
Parameter
year – The year specified as a value that is added to 1900 to get the actual year.
public String toGMTString();
public String toGMTString(); method returns a string representation of this Date object based on Internet GMT conventions. The string is of the form:
Sat, 8 Feb 2019 13:30:00 GMT
The date is the string is either one or two digits; the rest of the fields always have the width shown. The time zone
is always GMT.
This method returns a string that represents this Date.
public String toLocaleString();
public String toLocaleString(); method returns a string representation of this Date based on the conventions of the current locale.
This method returns a string that represents this Date.
public String toString();
public String toString() method returns a string representation of this Date. The string is of the form:
Sat Feb 8 2:30:00 MST 2019
This method returns a string that represents this Date.
Apart from these Date class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- notifyAll()
- wait()
- wait(long, int)
- getClass()
- notify()
- wait(long)