• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Getting a Date from a String

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This place is great!
Is there a method that I can pass the String "100404" or "10/04/04", (UK Style dates) and get back "Saturday, April 10, 2004" (US Style date)
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the java.text.DateFormat class...you'll probably want to use the SimpleDateFormat subclass:

sev
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that you may find taking a look at the API documentation for the SimpleDateFormat (and perhaps the DateFormat) and the Date classes that sever oon mentioned to be useful - especially for details on the date patterns available.
 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great sev and Dirk, that was very helpful!
Just one more thing:
The following code produces this output: Sat Apr 10 00:00:00 EDT 2004
Any idea how I could get rid of the "00:00:00 EDT"?

[ April 10, 2004: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Will,
You need to use two SimpleDateFormat objects. One to parse the input to make sure the date is in the correct format and the second one to print the date in the format you want.
First format you already got working. Second format should be,
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMMMMMMMM dd yyyy");
System.out.println(sdf.format(now));
Regards
Maulin
 
Will Carpenter
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome! thanks folks!
For those who had trouble following:
Here's the output: Sat Apr 10 2004
Given this code:
String eDate = "04 10 04";
try
{
Date fDate;
fDate = new SimpleDateFormat("MM dd yy").parse(eDate);
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("EEE MMMMMMMMM dd yyyy");
System.out.println(sdf.format(fDate));
}
catch(ParseException pex)
{ System.out.println(pex);
}
 
Do not threaten THIS beaver! Not even with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic