This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Need to change Calendar to dd/mm/yyyy format

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy cowboy,
Your proposal is good!

And your explanation is better!

Thanks.

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gamini,
Just share..
I hope this function help you more to understand how to interpret as a regular month number.
reply
    Bookmark Topic Watch Topic
  • New Topic