aspose file tools*
The moose likes Java in General and the fly likes date format issue. can you please tell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "date format issue. can you please tell " Watch "date format issue. can you please tell " New topic
Author

date format issue. can you please tell

swati chowdary
Greenhorn

Joined: Apr 07, 2005
Posts: 5
i want to have date object which has the format yyyy/mm/dd to insert into database

but using SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
i could format in the way i want but

DateFormat df = DateFormat.getDateInstance();
Date ddd=df.parse(enterdate);

and

Date startDate = cell.getDateCellValue();
String enterdate=new String(sdf.format(startDate).toString());

returns me string format but to convert it to again to date i used parse() and it returns me date if i print again it is printing in this format

Mon Mar 31 00:00:00 GMT+05:30 2008

instead yyyy/mm/dd.

i need this yyyy/mm/dd format if i print or to insert into data base.


thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Originally posted by swati chowdary:
i want to have date object which has the format yyyy/mm/dd to insert into database

Why not use a PreparedStatement instead? You can use its setDate methods combined with java.sql.Date objects. You can convert java.util.Date objects into these as follows:


DateFormat df = DateFormat.getDateInstance();

That will return you a locale specific date format. It's very unlikely that it will be the one you need; usually years are located at the end for these formats.

String enterdate=new String(sdf.format(startDate).toString());

You really really really want it to be a String, don't you?
sdf.format(startDate) returns a String.
Then you call toString, which return the same String object.
Then you create a new String with it, but it is equal to the earlier String.

So why not just use


but to convert it to again to date i used parse() and it returns me date if i print again it is printing in this format

Mon Mar 31 00:00:00 GMT+05:30 2008

instead yyyy/mm/dd.

That's because java.util.Date's toString method is called, and that prints the Date as you've shown. A Date object is little more than a timestamp with some methods around it. If you want to control its formatting when printing, that's exactly what DateFormat and its subclasses are there for. Well, that and parsing.

i need this yyyy/mm/dd format if i print or to insert into data base.

Either use a PreparedStatement as I've said before, or just use "enterdate" from the line I quoted before.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date format issue. can you please tell