| Author |
Date format... please help
|
Esa Bala
Greenhorn
Joined: Dec 23, 2009
Posts: 3
|
|
public static void main(String[] args) throws ParseException {
String str = "2010-06-24";
/*Req : The below sysout need to print date as 2010-06-24*/
System.out.println(formatDateAndTimeForDB1(str));
/*Getting output as Thu Jun 24 00:00:00 GMT+05:30 2010 : "HOW TO PRESERVE DATE FORMAT"*/
}
public static Date formatDateAndTimeForDB1(String date) throws ParseException{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date convertedDate = dateFormat.parse(date);
/*For Checking Purpose starts*/
String str = dateFormat.format(convertedDate);
System.out.println(str);/*output is 2010-06-24*/
/*For Checking Purpose ends*/
return convertedDate;
}
please provide me some possible solution for the above requirement.... its really urgent....
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
You're starting with a date string in 2010-16-24 format. You say you want it in 2010-16-24 format. Why are you trying to convert it? Just output the string. If that's not what you want to do, what is?
(Your formatDateAndTimeForDB1 method as it stands isn't formatting a date, it's parsing it and returning a Date object. You then write that to the output stream, so it uses the default format.)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
Date objects don't include a format. You need to format them to string, and print the string as output -- as you've done in the example.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Esa Bala
Greenhorn
Joined: Dec 23, 2009
Posts: 3
|
|
Henry Wong wrote:
Date objects don't include a format. You need to format them to string, and print the string as output -- as you've done in the example.
Henry
Thanks for your prompt response...
I need a date Object with format "yyyy-mm-dd". when i try to sysout the date object it should print in "2010-06-24" and not as "Thu Jun 24 00:00:00 GMT+05:30 2010". is it possible.
|
 |
Jim Hoglund
Ranch Hand
Joined: Jan 09, 2008
Posts: 525
|
|
Try returning str rather than convertedDate.
Jim ... ...
|
BEE MBA PMP SCJP-6
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
As Henry said, Date objects don't have a format. When you output them, you can use a formatter to get the result you want.
So:
Gives you a Date object. Then, to go back again:
But...since that gives you exactly what you started with, in that particular example you can replace all that with:
which suggests what you really need is something else.
|
 |
Esa Bala
Greenhorn
Joined: Dec 23, 2009
Posts: 3
|
|
Matthew Brown wrote:As Henry said, Date objects don't have a format. When you output them, you can use a formatter to get the result you want.
So:
Gives you a Date object. Then, to go back again:
But...since that gives you exactly what you started with, in that particular example you can replace all that with:
which suggests what you really need is something else.
Thanks Matthew...
But the problem is i need to populate an element in xml with date object having format as yyyy-mm-dd. so i am searching immensely to identify how to preserve the format of a date object with no luck...
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3793
|
|
Well, I've just showed you how to do that.
- Use a SimpleDateFormat to convert a String in a particular format to a Date (the parse() method).
- Use a SimpleDateFormat to convert a Date to a String in a particular format (the format() method).
The problem in your original code is that you start off with a String, convert this to a Date, convert this back to a String with the same format as you had in the first place, throw this second String away and return the Date object, then try to output this Date object without using a formatter.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
If you need a Date class, that has a format (you need a format for it to be preserved), then you can easily write one. You can subclass from the Date class, to have a class that has a format, and override the toString() method to use the format to convert it to a string.
Henry
|
 |
 |
|
|
subject: Date format... please help
|
|
|