Unparsable date exception

前端 未结 5 1177
我在风中等你
我在风中等你 2021-01-21 21:37

I\'m currently working on some simple project in Java and I have date in the following string:

String dateString = \"Sun 7/14 03:44 AM 2013\";


        
相关标签:
5条回答
  • 2021-01-21 21:53

    I tried this out and the following worked,

            String stringDate = "Sun 7/14 03:44 AM 2013";
            DateFormat format = new SimpleDateFormat("EEE MM/dd hh:mm a yyyy");
    
            System.out.println("Parsed Date = "+format.parse(stringDate));
    

    The output was as follows

    Parsed Date = Sun Jul 14 03:44:00 BST 2013
    
    0 讨论(0)
  • 2021-01-21 21:56

    The modern answer for the sake of completeness. While the other answers were good answers in 2013, Date, DateFormat and SimpleDateFormat are now long outdated, and I recommend you replace them with their modern counterparts:

        DateTimeFormatter parser 
                = DateTimeFormatter.ofPattern("EEE M/dd hh:mm a yyyy", Locale.ENGLISH);
        LocalDateTime ldt = LocalDateTime.parse(dateString, parser);
    

    The result is a LocalDateTime of 2013-07-14T03:44 as expected.

    The format pattern string is still the same, and the need for an English language locale is the same.

    0 讨论(0)
  • 2021-01-21 21:58

    Certain fields such as the day of week fields and/or AM/PM marker may not match those from your default Locale. ParseException has the method getErrorOffset to determine exactly where the pattern does not match.

    try

    DateFormat format = 
                    new SimpleDateFormat("EEE M/dd hh:mm a yyyy", Locale.ENGLISH);
    
    0 讨论(0)
  • 2021-01-21 21:58

    It is important to add Locale as you are parsing language day of week names.

    String dateString = "Sun 7/14 03:44 AM 2013";
    DateFormat format = new SimpleDateFormat("EEE M/dd hh:mm a yyyy", Locale.US);
    Date d = format.parse(dateString);
    
    0 讨论(0)
  • 2021-01-21 22:14
    SimpleDateFormat formatter = new SimpleDateFormat("/* type your own format*/");
    
    
    
      String formattedDate = formatter.format(todaysDate);
    
      System.out.println("Formatted date is ==>"+formattedDate);
    

    try this code

    0 讨论(0)
提交回复
热议问题