This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have used some calender usage etc(using some ones posting from the web) Following is my code: Calendar dateCreated = Calendar.getInstance(new Locale("en","US")); SimpleDateFormat df = new SimpleDateFormat("yyyy"); // apply the format object to the calendar object. String formattedDate = df.format(dateCreated.getTime());
But it is giving me bad date. Can some body suggest some thing easier. Kishore.
If you're just trying to get the current date, new Date() is much simpler than creating a Calendar. A Calendar is only necessary if you want to manipulate the individual fields like month, day of month, day of week, hourse, minutes, seconds, etc. However, I don't see anything actually wrong with your code either, so I'm not sure what the problem is.
"I'm not back." - Bill Harding, Twister
Joined: Jan 07, 2002
Well I'm curious what the default date of the calendar instance is. If you do a new GregorianCalendar(), I know it uses the current date. But I don't see any info in the JavaDocs about what the default date is when you do a Calendar.getInstance()..since I don't think I've ever used that method before.
Joined: Jan 30, 2000
The 1.3 API doesn't address this issue, but 1.4 makes it clear:
getInstance() Gets a calendar using the default time zone and locale. The Calendar returned is based on the current time in the default time zone with the default locale.
Joined: Jul 10, 2001
Rob, It gives like 08/32/.. which is not a valid date. I will try new Date() and see if that helps. If I grab "new Date()" does it help to format in a proper way like "04/19/2002". Thanks, Kishore.
Joined: Jan 30, 2000
It gives like 08/32/.. which is not a valid date. Ummm... how do you manage that? The code shown would only disply yyyy anyway - I assume you've changed other things too, to get 08/32/.. Perhaps you've confused M with m? If I grab "new Date()" does it help to format in a proper way like "04/19/2002". The new Date() has nothing to do with the formatting - it's just the simplest way to get today's date. No icky Calendars. Even with a Calendar, the Calendar doesn't control formatting either - that's done by the SimpleDateFormat. [ April 19, 2002: Message edited by: Jim Yingst ]
Calendar is a horror show. My current understanding is that most of it will be deprecated in 1.5, and relpaced with some more sane set of interfaces/classes. I'd suggest just using Date for now. Regards, Guy