• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to change Date Format

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic