Convertring data
Hi,
I have problem with convertring data... I get with my database date :
Date data = user.getDate();// return date object
now I must parse this date to format yyyy-mm-dd so :
DateFormat df = DateFormat.getDateInstance();
String sDate = df.format(data);
and format is ok.... but now I must convert this string to date because another metohod need object date in format (yyyy-mm-dd)... so I do :
Date dtTmp = new SimpleDateFormat("yyyy-mm-dd").parse(sDate);
System.out.print(dtTmp+"\n");
but when I print this date I get : Fri Jan 05 00:04:00 CET 2007 and the metod which use object data isn`t work because to good format must be 2007/mm/dd
How I can do this ?
[726 byte] By [
tomassa] at [2007-11-27 4:04:10]

> Hi,
>
> I have problem with convertring data... I get with my
> database date :
>
> Date data = user.getDate();// return date object
>
> now I must parse this date to format yyyy-mm-dd so :
>
> DateFormat df = DateFormat.getDateInstance();
> String sDate = df.format(data);
That will format the date into a locale specific format, so you should always specify the pattern to use.
> and format is ok.... but now I must convert this
> string to date because another metohod need object
> date in format (yyyy-mm-dd)... so I do :
>
> Date dtTmp = new
> SimpleDateFormat("yyyy-mm-dd").parse(sDate);
> System.out.print(dtTmp+"\n");
>
Take a look at the javadoc for SimpleDateFormat, your pattern is not correct.
Do also note that System.out.println on a Date always will print it in a locale specific format. A Date object does not contain format information.
> but when I print this date I get : Fri Jan 05
> 00:04:00 CET 2007 and the metod which use object data
> isn`t work because to good format must be 2007/mm/dd
>
> How I can do this ?
See above.
> Thank`s fo reply but I read :
>
> yyear(Number)
> 1996
> nth in year(Text & Number)July & 07
> dday in month(Number)
> 10
> mat y-M-d also don`t work....
Did you see what I said about System.out.println on a date? It will always print the same. You can't change the format of a Date.
Yes, you should use M instead of m for month.
Kaj
> So what is the way too see this date...
It looks like you don't understand what a date is. A date is milliseconds since 1 jan 1970, and it will always be that, it doesn't matter if you try to convert etc.
You should always use SimpleDateFormat when you want to convert to a String for presentation. toString on a date will always print the date in a locale specific format.
Kaj