Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to grab current date

 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what date is it giving you?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kishore,
If u using this :
SimpleDateFormat df = new SimpleDateFormat("yyyy");
U'll get only current Year.
As per Ur query u r doing this
SimpleDateFormat df = new SimpleDateFormat("yyyy/mm/dd");
This will give you Year/Minute/Date.

You will have to use
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
for the Year, Month and Day fields.
Hope It Helps,

Mahesh
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic