This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to grab current date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to grab current date" Watch "How to grab current date" New topic
Author

How to grab current date

Kishore Dandu
Ranch Hand

Joined: Jul 10, 2001
Posts: 1934
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.


Kishore
SCJP, blog
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
what date is it giving you?


Rob
SCJP 1.4
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jul 10, 2001
Posts: 1934
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Nov 24, 2000
Posts: 776
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

Joined: Jun 25, 2001
Posts: 110
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to grab current date
 
Similar Threads
Format Date using Calendar
How to get tomorrow's date?
!SimpleDateFormat
import java.util.date;
How to grab current date???