0

This is my date.
Fri, 25 Sep 2015 12:01:16 +0000

The output should look like this.
2015-09-25 12:01:16


This does not work.

SimpleDateFormat sdf = new SimpleDateFormat("E, d M y H:m:s X");

I need it to be converted to mysql date.

I have this error

Exception in thread "main" 
java.text.ParseException:Unparseable date: "25 Sep 2015 12:01:16"
at java.text.DateFormat.parse(DateFormat.java:366)
at Main.main(Main.java:27)
5
  • 4
    Whar do you mean by convert to mysql date? Commented Sep 27, 2015 at 20:19
  • 2
    What do you mean "does not work"? What happens? What do you do with the sdf after you define it? How do you convert it to java.sql.Date? Please show more code. Commented Sep 27, 2015 at 20:22
  • You might read this if you haven't already. Commented Sep 27, 2015 at 20:34
  • And/Or this Commented Sep 27, 2015 at 20:44
  • If your date has a FIXED format: WWW, DD MMM YYYY HH:MM:SS +NNNN" it might be better to use String` manipulation function called substring. Commented Sep 27, 2015 at 20:45

2 Answers 2

6

Your format is off, you need three M(s) to get the abreviated month name (using one gives the numeral value). Something like,

String str = "Fri, 25 Sep 2015 12:01:16 +0000";
SimpleDateFormat sdf = new SimpleDateFormat("E, d MMM y H:m:s X");
try {
    System.out.println(sdf.parse(str));
} catch (ParseException e) {
    e.printStackTrace();
}

And I get a valid Date. As for debugging the above, a quick and dirty way to verify your format is something like

System.out.println(sdf.format(new Date())); // <-- check your format pattern
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming fixed given date and time string of exactly this form--

WWW, DD MMM YYYY HH:MM:SS +NNNN

--this should work:

public class Date2Date
{
  static String month(String s)
  {
    switch(s.substring(8,11).toLowerCase()){
      case "jan": return "01";
        // etc.
      case "sep": return "09";
        // etc.
      case "nov": return "11";
    }
    return "12";
  }
    public static void main (String args []) {

      String str ="Fri, 25 Dec 2015 12:01:16 +0000";
                 //0123456789012345678901234567890      

      System.out.println(str.substring(12,16) + "-" + month(str) + "-" + 
                         str.substring(5,7) + " " + str.substring(17,25));
    }
}

If the fields are NOT fixed in length, read up on indexOf and other String functions. I think SOME editing of input has to be done to use SimpleDateFormat, but I could be wrong.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.