The Artima Developer Community
Sponsored Link

Java Answers Forum
Parsing Date/Time using SimpleDateFormat (or other)

3 replies on 1 page. Most recent reply: Jun 5, 2002 6:26 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
MikeD

Posts: 15
Nickname: mike
Registered: Apr, 2002

Parsing Date/Time using SimpleDateFormat (or other) Posted: Apr 29, 2002 11:34 AM
Reply to this message Reply
Advertisement
Hi,
I am having trouble parsing a Date/Time String into a Date object. It is no trouble to parse a date string, or to parse a date/time string with a parser that ignores the time portion, but when using a parser that should pick up the time portion, I get a ParseException.

Works:
try{
//SimpleDateFormat simp = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");
SimpleDateFormat parse = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String dateTime = "7/10/1996 4:50:00 PM";
Date parsed = parse.parse(dateTime);
System.out.println("formatted: " + formatter.format(parsed));
}
catch(ParseException e){
System.out.println("Caught " + e);
}


MikeD

Posts: 15
Nickname: mike
Registered: Apr, 2002

Full Question: Parsing Date/Time using SimpleDateFormat (or other) Posted: Apr 29, 2002 11:39 AM
Reply to this message Reply
Sorry, hit 'post' instead of 'preview'. Here is the full question.

Hi,
I am having trouble parsing a Date/Time String into a Date object. It is no trouble to parse a date string, or to parse a date/time string with a parser that ignores the time portion, but when using a parser that should pick up the time portion, I get a ParseException.

Works:
try{
//SimpleDateFormat simp = new SimpleDateFormat ("MM/dd/YYYY hh:mm:ss a");
SimpleDateFormat parse = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String dateTime = "7/10/1996 4:50:00 PM";
Date parsed = parse.parse(dateTime);
System.out.println("formatted: " + formatter.format(parsed));
}
catch(ParseException e){
System.out.println("Caught " + e);
}

Doesn't work:
try{
SimpleDateFormat parse = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");
//SimpleDateFormat parse = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String dateTime = "7/10/1996 4:50:00 PM";
Date parsed = parse.parse(dateTime);
System.out.println("formatted: " + formatter.format(parsed));
}
catch(ParseException e){
System.out.println("Caught " + e);
}

Any suggestions?

Thanks,
Mike

David Grant

Posts: 1
Nickname: daveg147
Registered: Jun, 2002

Re: Full Question: Parsing Date/Time using SimpleDateFormat (or other) Posted: Jun 4, 2002 8:45 PM
Reply to this message Reply
Hey Mike, I stumbled upon your post while looking for the answer to the same problem. Ten minutes later, I figured it out. I hope you already solved the problem, but just in case, here's what I did.

Make separate SimpleDateFormat objects for your date and time:

SimpleDateFormat df = new SimpleDateFormat ("M/d/yyyy");
SimpleDateFormat tf = new SimpleDateFormat ("h:mm a");

//output:
<%= df.format(myResult.getDate("date_time")) %>, <%= tf.format(myResult.getTime("date_time")) %>

HTH,
David

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Use lowercase yyyy for year and not uppercase YYYY Posted: Jun 5, 2002 6:26 AM
Reply to this message Reply
You made a simple mistake. While creating the first SimpleDateFormat object "parse" you are using uppercase YYYY for year. However, lowercase yyyy should be used here becase SimpleDateFormat doen't understand uppercase YYYY for year. That's it.
// Wrong one
SimpleDateFormat parse = new SimpleDateFormat("MM/dd/YYYY hh:mm:ss a");

// Right one
SimpleDateFormat parse = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");

Thanks
Kishori

Flat View: This topic has 3 replies on 1 page
Topic: Threads Previous Topic   Next Topic Topic: How can I do transform Applet to Application in this file..., ...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use