Date class keeps track of those milliseconds as a long value. Because long is a signed number, dates can be expressed before and after the start of January 1, 1970. The largest positive and negative
values expressible by the long primitive can generate dates forward and backward about 290,000,000 years, which suits most people's schedules. The Date class, found in the java.util package, encapsulates a long value representing a specific moment in time. One useful constructor is Date(), which creates a Date object representing the time the object was created. The getTime() method returns the long value of a Date object. In the program below, I use the Date() constructor to create a date based on when the program was run, and the getTime() method to find out the number of milliseconds that the date represents:
import java.util.*;
public class Now {
public static void main(String[] args) {
Date now = new Date();
long nowLong = now.getTime();
System.out.println("Value is " + nowLong);
}
}
When I ran that program, it gave me a value of 972,568,255,150. A quick check with my calculator confirms this number is at
least in the correct ballpark: it's a bit less than 31 years, which corresponds to the right number of years between January
1, 1970, and the day I wrote this article. While computers may thrive on numbers like the foregoing value, most people are
reluctant to say such things as "I'll see you on 996,321,998,346." Fortunately, Java provides a way to convert Date objects to Strings, which represent dates in more traditional ways. The DateFormat class, discussed in the next section, can create Strings with alacrity.
DateFormat class is to create Strings in ways that humans can easily deal with them. However, because of language differences, not all people want to see a date
in exactly the same way. Someone in France may prefer to see "25 decembre 2000," while someone in the United States may be
more accustomed to seeing "December 25, 2000." So when an instance of a DateFormat class is created, the object contains information concerning the particular format in which the date is to be displayed.
To use the default format of the user's computer, you can apply the getDateInstance method in the following way to create the appropriate DateFormat object:
DateFormat df = DateFormat.getDateInstance();
The DateFormat class is found in the java.text package.
You can convert a Date object to a string with the format method. This is shown in the following demonstration program:
import java.util.*;
import java.text.*;
public class NowString {
public static void main(String[] args) {
Date now = new Date();
DateFormat df = DateFormat.getDateInstance();
String s = df.format(now);
System.out.println("Today is " + s);
}
}
The getDateInstance method shown in the code above, with no arguments, creates an object in the default format or style. Java also provides some
alternative styles for dates, which you can obtain through the overloaded getDateInstance(int style). For convenience' sake, DateFormat provides some ready-made constants that you can use as arguments in the getDateInstance method. Some examples are SHORT, MEDIUM, LONG, and FULL, which are demonstrated in the program below:
Good ariticleBy Anonymous on June 28, 2009, 7:45 amGood ariticle
Reply | Read entire comment
this article rocksBy Anonymous on June 26, 2009, 1:18 pmwow, I could never have figured this out myself. You helped me solve all sorts of problems here. It's now nine years in the future since you wrote this too.
Reply | Read entire comment
ThanksBy Anonymous on June 4, 2009, 2:23 pmSuperb Article. An article with profound meaning/content and simple to understand. I thought handling dates in java are tough. But now I feel, I can start playing...
Reply | Read entire comment
Nice articleBy Anonymous on February 17, 2009, 7:03 pmNice article
Reply | Read entire comment
Nice Article. Helped a lot for my Internationalization Problem..By hemanthjava on February 10, 2009, 3:53 amNice Article. Helped a lot for my Internationalization Problem. Regards, Nuklear iwebie.com
Reply | Read entire comment
View all comments