date in format yy-mm-dd hh:mm

S

Sigi

Hi,
I have a Date object, and I need to obtain it in a human readable format.
If I use date.toString(), it gives:
Fri Jan 26 17:47:57 CET 2007

What can I do to obtain it in the
07-01-26 17:47
format?

Thanks.
 
W

www

Sigi said:
Hi,
I have a Date object, and I need to obtain it in a human readable format.
If I use date.toString(), it gives:
Fri Jan 26 17:47:57 CET 2007

What can I do to obtain it in the
07-01-26 17:47
format?

Thanks.

My program does this everyday. What I did was:


String timeString = null;

SimpleDateFormat utc = new SimpleDateFormat("yy-MM-dd hh:mm"); //this
is the trick. You can set it "yyyy-MM-dd hh:mm:ss". y and Y means
different, and M & m, d &D, check Java API.
utc.setTimeZone(TimeZone.getTimeZone("UTC")); //set time zone
timeString = utc.format("your Date obj");
 
L

Lew

This is a very non-standard date format. Why choose this particular one?

N.b., whether a format is standard really only matters if you are sharing
results with others, but I am curious nonetheless.

- Lew
 
T

Thomas Fritsch

Sigi said:
I have a Date object, and I need to obtain it in a human readable format.
If I use date.toString(), it gives:
Fri Jan 26 17:47:57 CET 2007

What can I do to obtain it in the
07-01-26 17:47
format?
DateFormat dateFormat = new SimpleDateFormat("yy-MM-DD HH:mm");
// You'll find more info about the pattern strings
// in the API doc of SimpleDateFormat

Date date = ...;
String s = dateFormat.format(date);
 
M

Mark Rafn

Lew said:
This is a very non-standard date format. Why choose this particular one?

If it were 2007-01-26 17:47 it would be a fairly common format for
timestamping log entries. It has a very large advantage over other standards
in that that it's ascii-sortable so it is trivial to merge log records and
preserve temporal sequence (assuming you're using UTC as your timezone;
daylight savings can mess this up otherwise).

Dropping the century makes it suck, though, because it's then gets
confusing whether you're using this format, or the common US/Euro
shorhand m-d-y and d-m-y.
 
L

Lew

Mark said:
If it were 2007-01-26 17:47 it would be a fairly common format for

<http://en.wikipedia.org/wiki/ISO_Date_Format>

Use of the space character ' ' in lieu of the 'T' between the date and time is
widely accepted for practical use.
Dropping the century makes it suck, though, because it's then gets
confusing whether you're using this format, or the common US/Euro
shorhand m-d-y and d-m-y.

or if so, which one:

07-01-26 =>
July 1, ??26 vs. 7 January, ??26.

Except that these formats really use '/' not '-'.

- Lew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top