2
votes

What is the Java DateTime format for this one?

Mon Nov 26 13:57:03 SGT 2012

I want to convert this string to Date and convert it to another format like "yyyy-MM-dd HH:mm:ss".

To convert from date to string is not hard.

Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

But I find no valid format to convert "Mon Nov 26 13:57:03 SGT 2012" to become date format...

=====

found solution:

DateFormat oldDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Format newDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date oldDate = oldDateFormat.parse(oldTimeString);
String newDateString = newDateFormat.format(oldDate);
4
now jon skeet will come and tell you to use Joda time. =)Juvanis
@ivanovic: Yes in general, but I'm not sure that Joda Time can cope with time zone abbreviations when parsing...Jon Skeet
@JonSkeet I haven't used Joda time. But thanks for making my oracle (my previous comment) true =)Juvanis
@Iannyboy, can you please accept an answer if any helped you?Reddy
See here: stackoverflow.com/a/11097520/2895571 This should solve your problem..pavle

4 Answers

4
votes

This will work, EEE MMM dd HH:mm:ss z yyyy

You can find examples in the javadoc of SimpleDateFormat. See http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

3
votes

Try SimpleDateFormat.parse() function to convert the string to Date.

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date parseDate = sdf.parse(strInput);

Watch out for the Parse Exception

3
votes

Well, this code produces some output

import java.util.*;
import java.text.*;

public class Test {

    public static void main(String[] args) throws ParseException {
        DateFormat inputFormat = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy",
                                                      Locale.US);
        DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                                                       Locale.US);
        String text = "Mon Nov 26 13:57:03 SGT 2012";

        Date date = inputFormat.parse(text);
        System.out.println(outputFormat.format(date));
    }
}

... but it uses the default system time zone for output. It's not clear what time zone you want the result in. There's nothing in Date to store the time zone, which makes it hard to preserve the original time zone given in the text, so you'll need to decide for yourself which zone to use.

Note that I've specified Locale.US in both input and output; that's typically appropriate when you're specifying a custom format, particularly for the input which relies on month and day names.

As noted in comments, I would personally recommend using Joda Time if you possibly can for date/time work... it's a far better API than Date/Calendar. Unfortunately, Joda Time is incapable of parsing time zones - from the docs for DateTimeFormat:

Zone names: Time zone names ('z') cannot be parsed.

It's also worth noting that if there's any way you can affect the input data, moving them away from using time zone abbreviations would be a good step.

0
votes
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CPDateTime 
{
    public static void main(String[] args)
    {
        Calendar cal  = Calendar.getInstance();
        //subtracting a day
        //cal.add(Calendar.DATE, -1);
        cal.add(Calendar.MONTH, -1);
        SimpleDateFormat prev_day = new SimpleDateFormat("dd");
        SimpleDateFormat prev_month = new SimpleDateFormat("MM");
        SimpleDateFormat prev_year = new SimpleDateFormat("YYYY");   
        String prev_day_str = prev_day.format(new Date(cal.getTimeInMillis()));
        System.out.println(prev_day_str);
        String prev_month_str = prev_month.format(new Date(cal.getTimeInMillis()));
        System.out.println(prev_month_str);
        String prev_year_str = prev_year.format(new Date(cal.getTimeInMillis()));
        System.out.println(prev_year_str);
    }
}