Java Reference
In-Depth Information
If the number of arguments is less than two, one of the first three forms is used. If the
number of arguments is two or more, the fourth form is used. If no argument is passed,
it creates a Date object with the current date and time. If only one number argument is
passed, it is considered the number of milliseconds passed since midnight January 1,
1970 UTC. If only one string argument is passed, the date and time in the string is parsed
and used as the date and time value for the new Date object. If two or more arguments are
passed, a Date object is constructed; remaining values are set to 1 for day and 0 for hours,
minutes, seconds, and milliseconds. The Date object has a static method Date.now() that
returns the number of milliseconds elapsed between the midnight January 1, 1970 UTC
and the current time.
The following code shows how to create Date objects passing different number of
arguments to the constructor:
// Current date and time
print("new Date() =", new Date());
// Pass 2000 milliisesonds
print("new Date(2000) =", new Date(2000));
// Pass 2000 milliisesonds and convert to UTC date time string
print("new Date(2000).toUTCString() =", new Date(2000).toUTCString());
// Pass a date as a string. Date is considered in UTC
print('new Date("2014-14-04") =', new Date("2014-14-04"));
// Pass year and month. day will default to 1
print("new Date(2014, 10) = ", new Date(2014, 10));
// Pass year, month. day, and hour. Other parts will default to 0
print("new Date(2014, 10, 4, 10) = ", new Date(2014, 10, 4, 10));
// Milliseconds elapsed from midnight Januray 1070 and now
print("Date.now() = ", Date.now());
new Date() = Sat Oct 04 2014 19:38:46 GMT-0500 (CDT)
new Date(2000) = Wed Dec 31 1969 18:00:02 GMT-0600 (CST)
new Date(2000).toUTCString() = Thu, 01 Jan 1970 00:00:02 GMT
new Date("2014-14-04") = Fri Oct 03 2014 19:00:00 GMT-0500 (CDT)
new Date(2014, 10) = Sat Nov 01 2014 00:00:00 GMT-0500 (CDT)
new Date(2014, 10, 4, 10) = Tue Nov 04 2014 10:00:00 GMT-0600 (CST)
Date.now() = 1412469527013
There are over forty methods in the Date objects. I will briefly explain them.
These methods work the same way, although on different components of the date
and time. It contains getYear() , getFullYear() , getDate() , getMonth ( ), getHours() ,
getMinutes() , getSeconds() , and getMilliseconds() method that returns the 2-digit
year, full year, date, month, hours, minutes, seconds, and milliseconds components of
 
Search WWH ::




Custom Search