aspose file tools
The moose likes Java in General and the fly likes java.util.Calendar Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "java.util.Calendar" Watch "java.util.Calendar" New topic
Author

java.util.Calendar

Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
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
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:

If you want more control over the format, try using a SimpleDateFormat object. You can find more info on this stuff in the API. http://java.sun.com/j2se/1.3/docs/api/index.html
Nick
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
// Format the current time.
SimpleDateFormat formatter = new SimpleDateFormat ("EEE MM dd, yyyy");
Date currentDate1 = new Date();
String dateString = formatter.format(currentDate1);


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jay Brass
Ranch Hand

Joined: Oct 24, 2000
Posts: 76
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
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
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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Drinks are on the house!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: java.util.Calendar
 
Similar Threads
Calendar() / Date() bug ???
Date
Gregorian roll() function- April Fools Bug??
Calculation of months between two dates
Calender class and its components