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]
# 1

> 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.

kajbja at 2007-7-12 9:09:01 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank`s fo reply but I read : yyear(Number)1996 Mmonth in year(Text & Number)July & 07 dday in month(Number)10So.... format y-M-d also don`t work....
tomassa at 2007-7-12 9:09:01 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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

kajbja at 2007-7-12 9:09:01 > top of Java-index,Java Essentials,Java Programming...
# 4
So what is the way too see this date... convertring to string ? System.out.print(sDate.valueOf(dtTmp)+"\n");StringDateI can`t see diferences...
tomassa at 2007-7-12 9:09:01 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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

kajbja at 2007-7-12 9:09:01 > top of Java-index,Java Essentials,Java Programming...