Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

date format issue. can you please tell

 
swati chowdary
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic