Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change Date Format

 
Uma Vinodh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to change the date format from yyyy-MM-dd to MM/dd/yyyy.

Here is my code:

SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
String s=sdf.format(2006-06-05);
Date d2= sdf.parse(s);

The result with this code is :Mon Jun 05 00:00:00 CDT 2006

I don't know what I did wrong in this?

can anyone suggest me what to do to change the format and get 06/05/2006?

Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Date objects do not contain any formatting. The only way to get a Date object formatted is using the format method of a DateFormat, and use the String result.
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume your input is a String representing a date in yyyy-MM-dd format and you want your output to be a String of that same date, but in MM/dd/yyyy format. In that case, you'll need two SimpleDateFormat objects--one to parse the yyyy-MM-dd String into a Date object and another to format that Date object into a MM/dd/yyyy String.
 
Uma Vinodh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael and Rob,

Thanks for responding.

i want my output to be date in MM/dd/yyyy format.

what I did is..

1.My input is date in yyyy-MM-dd format.

2.I convertes that into String using format function. now the result is string which contains date in MM/dd/yyyy format.

3.Now I convert that String to date,so I used parse function but the result is in different format.

Hope this helps you to solve my problem.
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You do know that in this line of code, "2006-06-05" is not a date. It is a mathematical expression -- which is resolved at compile time.

2006-06-05 = 2006 minus 06 (which is 6 in decimal) minus 05 (which is 5 in decimal) = 1995.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As others already mentioned, a date object doesn't have a format. The purpose of the simple date format class is to format or parse the format, when it is converted to or from a string. All the formatting is on the string, the date object doesn't have any formatting.

But....


1.My input is date in yyyy-MM-dd format.


Acutally, no. You "date" is a integer, which is valued at 1995.


2.I convertes that into String using format function. now the result is string which contains date in MM/dd/yyyy format.


Which I am surprised actually compiled, as there isn't a format() method that takes an int. But yes, assuming that you have a properly formatted string, you can get the date with the parse() method.

3.Now I convert that String to date,so I used parse function but the result is in different format.


Again, a date object doesn't have a format. Once it is a date, you need to format() it with the simple date format, to get back a string, if you want to print it.

Henry
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uma Vinodh wrote:My input is date in yyyy-MM-dd format.

Like Rob said, this doesn't make sense. A Date object doesn't have a format. Are you sure it's not a String?
Henry Wong wrote:You do know that in this line of code, "2006-06-05" is not a date. It is a mathematical expression

...which resolves to an integer, which format() does not accept, and which should fail at compile time.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Which I am surprised actually compiled, as there isn't a format() method that takes an int.

Format has a method format(Object), so the int is autoboxed into an Integer.

This method calls format(Object, StringBuffer, FieldPosition) which is overridden (implemented actually) by DateFormat with the following body:
So not only will it compile, it will even run! The results will not be what you'd expect though.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic