• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.util.Calendar

 
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// Format the current time.
SimpleDateFormat formatter = new SimpleDateFormat ("EEE MM dd, yyyy");
Date currentDate1 = new Date();
String dateString = formatter.format(currentDate1);
 
Jay Brass
Ranch Hand
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Drinks are on the house!
 
reply
    Bookmark Topic Watch Topic
  • New Topic