tags:

views:

5623

answers:

5

Is there a way to format a UTC time into any arbitrary string format I want in java? Basically I was thinking of having some class take the timestamp and I pass it is string telling it how I want it formated, and it returns the formatted string for me. Is there a way to do this?

+10  A: 

SimpleDateFormat. The javadoc header explains it better than I could.

bcash
+4  A: 

The DateFormat class or SimpleDateFormat should get you there. For example, http://www.epochconverter.com/ lists the following example to convert a epoch time to human readable timestamp with Java:

String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));
Jay
+3  A: 

Dates are totally broken in Java. Sun screwed is twice (first with java.util.Date and second with java.util.Calendar).

Use Joda Time instead. It's done right. you will never have to look back at JDK Dates.

...You may ask what about database access, say, via Hibernate? Don't worry, Joda time integrates with Hibernate too.

Maxim
+2  A: 

One gotcha to be aware of is that SimpleDateFormat is NOT thread-safe. Do not put it in a static field and use it from multiple threads concurrently.

Alex Miller
A: 

"Java dates are totally broken" ??? Except that lots of people use them w/out complaint.

Bob White