convert String to Date in android

12,105

z expects the format GMT+02:00 not GMT+02.00 replace the . and it should work.

Worked for me with the following code:

    SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
    try {
        String temp = "Fri Jul 30 16:19:36 GMT+02:00 2021";
        Date expiry = formatter.parse(temp);
    } catch (Exception e) {
        e.printStackTrace();
    }
Share:
12,105
catherine8473
Author by

catherine8473

Updated on June 18, 2022

Comments

  • catherine8473
    catherine8473 almost 2 years

    anyone know how to convert the following string to Date?

    "Fri Jul 30 16:19:36 GMT+02.00 2021"

    i tried:

     SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
    try {String temp = value2[i].trim();
         expiry = formatter.parse(temp);
    
        } catch (Exception e) {
        e.printStackTrace();
        }
    

    but,it doesn't work.

  • catherine8473
    catherine8473 almost 13 years
    will,,after i replace,,the string is "Fri Jul 30 16:19:36 GMT+02:00 2021",,but the parse gives the error: "java.text.ParseException: Unparseable date: Fri Jul 30 16:19:36 GMT+02:00 2021"
  • MByD
    MByD almost 13 years
    It is parsed well in my side, I'll post the code, so you might see some difference
  • catherine8473
    catherine8473 almost 13 years
    i solve the problem in this way: first,,replace "." to ":" as u indicated before; secondly, change the constructor in this way: "SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy",Locale.US);",,,,then it works,,,,,