Java date formatting

D

dufffman

Hi,

I am querying a sybase database to and getting back a as a part of a
resultset a field that is stored as datetime in the database.

Once I get it out to a date object, the value of my
dateObject.toString() is
"Tue Oct 01 00:00:00 EDT 2002".

I want to convert this to yyyyMMdd (20021001) format. How do I achieve
this?

This is what I have tried

Date date = // Date object from sybase
SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
String stringDate = date.toString();

try {
date = df.parse(stringDate);
}
catch(ParseException e) {
System.out.println("Unable to parse " + stringDate + " " +
e.getErrorOffset());
}

The above code throws the ParseException w/error offset 0. Any ideas?

Thanks,
 
B

Bjorn Abelli

I am querying a sybase database to and getting back a as a part of a
resultset a field that is stored as datetime in the database.

Once I get it out to a date object, the value of my
dateObject.toString() is
"Tue Oct 01 00:00:00 EDT 2002".

I want to convert this to yyyyMMdd (20021001) format.
How do I achieve this?

This is what I have tried

Date date = // Date object from sybase
SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
String stringDate = date.toString();

try {
date = df.parse(stringDate);
}
catch(ParseException e) {
System.out.println("Unable to parse " + stringDate + " " +
e.getErrorOffset());
}

The above code throws the ParseException w/error offset 0. Any ideas?

There's no reason to first get a default formatted String from a Date and
then parse that String *back* to a Date in order to do... what?

Isn't this what you want?

Date date = // Date object from sybase
SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
String stringDate = s.format(date);


/// Bjorn A





Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
J

John O'Conner

Hi,

I am querying a sybase database to and getting back a as a part of a
resultset a field that is stored as datetime in the database.

Once I get it out to a date object, the value of my
dateObject.toString() is
"Tue Oct 01 00:00:00 EDT 2002".

I want to convert this to yyyyMMdd (20021001) format. How do I achieve
this?


See the following:
* Java Tutorial for Customizing Date Formats
http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html

* Formatting Dates Around the World
http://www.joconner.com/javai18n/articles/DateFormat.html

Regards,
John O'Conner
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top