This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes A kinder, gentler java.util.Date? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A kinder, gentler java.util.Date?" Watch "A kinder, gentler java.util.Date?" New topic
Author

A kinder, gentler java.util.Date?

Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
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.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Try

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
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A kinder, gentler java.util.Date?
 
Similar Threads
Need help w/ timestamp incl in log file names.
problem in date
How to display date in Daylight Savings
Date conflict
problem displaying jsp