Hey there everyone, This code: public static void main(String arg[]) { Date date = new Date(); System.out.println(date); } prints: Wed Nov 01 12:11:24 EST 2000 (actually whatever the present date/time is when it is run) Knowing that many of the date methods have been deprecated in favor of methods in the Calendar class, how do I get just the day month date and year to print? ex. Wed Nov 01, 2000 That's all I want. Is there a date format in the calendar class to parse out the date like the above? thx in advance J
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
You can probably get what you want out of the DateFormat class. It has a bunch of static methods that return formatters for just this kind of stuff. It's really useful for internationalization of dates and times (you can pass your formatter a locale and let it do the rest) but there are methods which accept the default locale and then format your date into a date string. It would look something like this:
// Format the current time. SimpleDateFormat formatter = new SimpleDateFormat ("EEE MM dd, yyyy"); Date currentDate1 = new Date(); String dateString = formatter.format(currentDate1);
Thanks Nick, I did some tinkering of my own and came up with this: Calendar cal = Calendar.getInstance(); String mondayyear = "MMM dd, yyyy"; java.text.SimpleDateFormat dateformat = new java.text.SimpleDateFormat(mondayyear); System.out.println(dateformat.format(cal.getTime())); Which effectively gives me: Nov 01, 2000 I plugged your code in with a few minor changes: System.out.println(DateFormat.getDateInstance(DateFormat.FULL).format(cal.getTime())); and got what I wanted: Wednesday, November 1, 2000 Thanks alot. J
Jay Brass
Ranch Hand
Joined: Oct 24, 2000
Posts: 76
posted
0
Paul, Your way gives me the month in 3 letters. That is more what I was looking for but was willing to settle for the other way. You guys rule! J
Jay Brass
Ranch Hand
Joined: Oct 24, 2000
Posts: 76
posted
0
My apologies Thomas. As you are the bartender I should probably address you by your first name. Again many thanks for all your help. J