datetime formatting

Hi guys,

How do I create a datetime that looks like this:
Wed, 9 jan 2004 19:59:09 GMT

into a string?

The date should reflect the current date time on which it was created.
LVL 2
DaFouAsked:
Who is Participating?
 
CEHJCommented:
                 DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
                  System.out.println(df.format(new java.util.Date()));
0
 
Tommy BraasCommented:
You would have to create a Calendar object with the right time, and then use CEHJ's code to output it into the format requested:
// the time in your code
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR 2004);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DAY_OF_MONTH 9);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 9);
0
 
Tommy BraasCommented:
To then ge the Date object needed by the DateFormat you need to call:
Date date = calendar.getTime();
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.