java.text.ParseException: Unparseable date



  • I've got a problem, but the next java isn't working.

    String s_date = " Ср, 16 12 2015 16:24:31 GMT";
    SimpleDateFormat sdf = new SimpleDateFormat( "     dd MM yyyy hh:mm:ss    " );
    Date date = sdf.parse( s_date );
    System.out.println( sdf.format( date ) );
    

    Turns out:

    java.text.ParseException: Unparseable date: " Ср, 16 12 2015 16:24:31 GMT"
    

    You can cut the first five symbols and the last four. But I'll have many different formats of dates and every different approach. How do we make SimpleDateFormat.parse(s) pass what's said?

    If you can't see it in the code, I set five gaps at the beginning and four at the end, it didn't work.



  • Specifically, in your case, you need to adjust the format line:

        // было: "dd MM yyyy hh:mm:ss"
        // модификации: EEE - день недели строкой, HH - час 0-23, zzz - временная зона
        SimpleDateFormat sdf = new SimpleDateFormat( " EEE, dd MM yyyy HH:mm:ss zzz", new Locale("ru", "RU") );
        Date date = sdf.parse( s_date );
        System.out.println( sdf.format( date ) ); //Вывод:  Ср, 16 12 2015 19:24:31 MSK
    

    New API:

        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern( "E, d M y H:m:s z", new Locale("ru", "RU") );
        ZonedDateTime newDate = ZonedDateTime.parse( s_date.trim(), newFormatter );
        System.out.println( newFormatter.format( newDate )); // Вывод: Ср, 16 12 2015 16:24:31 GMT
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2