aspose file tools*
The moose likes Java in General and the fly likes Need to change Calendar to dd/mm/yyyy 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 » Java in General
Bookmark "Need to change Calendar to dd/mm/yyyy format" Watch "Need to change Calendar to dd/mm/yyyy format" New topic
Author

Need to change Calendar to dd/mm/yyyy format

Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
Hi All,

Am having a string which is in dd/mm/yyyy format which I had parsed to Date and then to Calendar and written some logic.
Now am not able to change the output of Calendar to dd/mm/yyyy format.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

A Calendar does not have a format itself.

You used SimpleDateFormat to parse a date string into a Date object (which you then used to set the calendar with). To convert a date back to a string, you use the SimpleDateFormat object again, but you call format(...) instead of parse(...). For example:



Note that the method getTime() of class Calendar gives you a Date object, which you can pass to the format(...) method of class SimpleDateFormat to convert it to a String.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 350
Hi guys,
Couldn't figure this out yet.

I get the following when I run this modified.. why is there a difference in the month?

27/08/2009
27/7/2009



Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14270
    
  21

Is there a reason why you don't want to do it with a SimpleDateFormat object as I proposed ?

Months in the Java Calendar class are 0-based: 0 = January, 1 = February, ..., 7 = August, ..., 11 = December. So if you do cal.get(Calendar.MONTH), you'll get a number from 0 to 11, not 1 to 12. You need to add 1 yourself if you want to interpret this as a regular month number yourself.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 350
Howdy cowboy,
Your proposal is good!

And your explanation is better!

Thanks.

Ramses Butarbutar
Ranch Hand

Joined: Jun 16, 2009
Posts: 52
Gamini,
Just share..
I hope this function help you more to understand how to interpret as a regular month number.


At the center of non-violence stands the principle of love
.... Martin Luther King, Jr.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need to change Calendar to dd/mm/yyyy format