Date Formatting with Java

N

Nino Skilj

I have been struggling with this for while now and I have determined
that I just don't get it!

I have a date as a string in this format: Mon Jun 30 16:45:06
I want to format it as a date like this: 2003-06-30 (yyyy-MM-dd)

This is what I have tried so far (about the 20th iteration of it at
least):

String DATE_FORMAT = "yyyy-MM-dd";
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

DateFormat df = DateFormat.getDateInstance();
try {
Date xyz = df.parse(fixdates.getString("fulldate"));
} catch (ParseException e) {
out.print("Parse Exception")
}

String formatteddate = formatter.format(xyz);

Any ideas? The code compiles and I always get a:
Parse Exception Error: Unparseable date: "Mon Jun 30 16:45:06"

What am I doing wrong?

Nino Skilj
 
E

Emery Z. Balint Jr.

By the looks of it, your original date is missing the year. I wonder if that
is throwing the exception. Try including a year and see if that works.

Emery.
/\^/\^/\
North Star Bulletin Board System
The Java Based Intelligent Web Forum
http://www.northstarbbs.com/
 
P

P.Hill

Nino said:
String DATE_FORMAT = "yyyy-MM-dd";
SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

DateFormat df = DateFormat.getDateInstance();
try {
Date xyz = df.parse(fixdates.getString("fulldate"));

getDateInstance() returns the a DateFormat for the standard date format
for your locale. Apparently that is NOT compatable with
"Mon Jun 30 16:45:06"

What made you think it would just wander around in your string and
be able to find the various fields?

You need to make up you OWN format with
new SimpleDateFormat( "...." );

So just do what you thought of doing for your output format
and do it for the input also.

This can work even if it doesn't include the year. You'll always get the
current year, but maybe that is sufficient, but consider
what happens around the new year.

try
http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
for ideas of what to put in the String sent to SimpleDateFormat.

HTH,
-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,277
Latest member
VytoKetoReview

Latest Threads

Top