This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, I'm wondering if there's a simpler way to return the current date and time from the system than what I'm doing right now. I'm really hoping there's something analogous to the "Date()" I was using when I was writing ASP. This is my code:
I want the date and time to appear in a friendly format (not milliseconds since jan 1, 1970, or whatever it is). It seems like that would be a common need, and there would be a quick way to address it. Am I missing something? Thanks, g.
Perhaps you are looking for something like SimpleDateFormat. Try playing with this:
[ February 05, 2002: Message edited by: Cindy Glass ]
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Jan 28, 2002
Cindy, Thanks for the quick reply. What I'm looking for is a way to get around needing both a Date and a DateFormatter. I've been to everything I could think of in the API, but I can't find a method in the Date class that returns anyting but the entire date/time in a String (no good, because I need to display the two in different table cells) or integers representing the seperate elements of the date. It just seemed like there would be a shortcut for such a common operation.
With no arguments, the Date() constructor uses the current system time. And the toString() method will give you a reasonably nice display, depending on your locale and system. On my system, I get
If you don't like the format you get, then yes you will need to use some sort of DateFormat to fix it - but chances are good you won't have to. [ February 05, 2002: Message edited by: Jim Yingst ]
"I'm not back." - Bill Harding, Twister
Joined: Jan 28, 2002
Thank you both. I ended up just using Jakarta's datetime tag library, since I wanted something neat and clean to place on multiple pages. But I have another question: how do you get the difference between two dates? I need to get someone's current age. I try subtracting, and it gives me a conversion error. The Date.compareTo() method gives me integers I assume represent greater than, less than, or equal to. How can I get the time difference, rounded to years? ..Nevermind. I just subtracted getYear() from getYear(). Thanks again. g. [ February 05, 2002: Message edited by: Garann Rose Means ]