File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A kinder, gentler java.util.Date?" Watch "A kinder, gentler java.util.Date?" New topic

A kinder, gentler java.util.Date?

Garann Means
Ranch Hand

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

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
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

Joined: Jan 30, 2000
Posts: 18671

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.
[ February 05, 2002: Message edited by: Garann Rose Means ]
I agree. Here's the link:
subject: A kinder, gentler java.util.Date?
It's not a secret anymore!