datetime formatting

Posted on 2006-04-08
Last Modified: 2010-03-31
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.
Question by:DaFou
    LVL 86

    Accepted Solution

                     DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz");
                      System.out.println(df.format(new java.util.Date()));
    LVL 14

    Expert Comment

    by:Tommy Braas
    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.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);
    LVL 14

    Expert Comment

    by:Tommy Braas
    To then ge the Date object needed by the DateFormat you need to call:
    Date date = calendar.getTime();
    LVL 86

    Expert Comment


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VB Script 3 60
    method argument as final 1 54
    Logs to delete in Windows VCenter server ? 11 92
    countHi challenge 25 62
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now