Java String to date format

J

JD Uhler

I want to convert the String "2001-09-07 00:00:00.0" into the format
"09/07/2001". How do I do that without having to do a bunch of string
manipulations? Can I use SimpleDateFormat to just convert it to a
string? I have tried that, but an error comes back saying something
like "Cannot convert Object to Date".

The original String can be null as well.

Thank you

JD
 
J

John O'Conner

JD said:
I want to convert the String "2001-09-07 00:00:00.0" into the format
"09/07/2001". How do I do that without having to do a bunch of string
manipulations? Can I use SimpleDateFormat to just convert it to a
string? I have tried that, but an error comes back saying something
like "Cannot convert Object to Date".

The original String can be null as well.

Thank you

JD


Use a DateFormat to parse() this String to a Date object. Use a
different DateFormat to format() the Date to the String representation
you want.

See http://www.joconner.com/javai18n/articles/DateFormat.html for more
information.

Regards,
John O'Conner
 
J

John O'Conner

JD said:
I want to convert the String "2001-09-07 00:00:00.0" into the format
"09/07/2001". How do I do that without having to do a bunch of string
manipulations? Can I use SimpleDateFormat to just convert it to a
string? I have tried that, but an error comes back saying something
like "Cannot convert Object to Date".

The original String can be null as well.

Thank you

JD


Another (easier/quicker) option is to use String's split() method to
create an array containing the substrings "2001", "09", and "07". Now
you can create a new String using these substrings. There are a few
string manipulations, but they are simple.

Regards,
John O'Conner
 
J

JD Uhler

While we are on the topic of dates.. are there any methods/properties
that tell you which fiscal year you are currently in? I was going to
create the whole "greater/less than July 1st" routine, but thought
there may be an easier way.

Thank you

JD
 
P

P.Hill

JD said:
While we are on the topic of dates.. are there any methods/properties
that tell you which fiscal year you are currently in? I was going to
create the whole "greater/less than July 1st" routine, but thought
there may be an easier way.

I assume you already are aware a the Calendar/GregorianCalendar class.

-Paul
 
J

John O'Conner

P.Hill said:
I assume you already are aware a the Calendar/GregorianCalendar class.

-Paul



Although Paul directs you to the Calendar class, which can help you add,
subtract, and roll dates around, Calendar is unaware of domain specific
terms like "fiscal year". You would have to create that class yourself,
but certainly that class could use Calendar just as Paul suggests.
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top