aspose file tools*
The moose likes Beginning Java and the fly likes How to change Date Format Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to change Date Format" Watch "How to change Date Format" New topic
Author

How to change Date Format

Uma Vinodh
Greenhorn

Joined: Apr 26, 2006
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

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.


SCJP 6 || SCWCD 5
Uma Vinodh
Greenhorn

Joined: Apr 26, 2006
Posts: 28
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Jun 17, 2009
Posts: 274

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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change Date Format