Unparseable date: “30-Jun-12”

后端 未结 3 607
萌比男神i
萌比男神i 2021-01-22 11:44

I\'m using a Java method to convert the date into suitable format:

private Timestamp toTimeStamp(String s) throws java.text.ParseException
    {
        Timestam         


        
相关标签:
3条回答
  • 2021-01-22 11:54

    You are passing date in format 30-Jun-12 then you should use format

    dd-MMM-yy
    

    with SimpleDateFormat

    0 讨论(0)
  • 2021-01-22 11:58

    There are 2 problems:

    1. Your date format pattern is wrong. The yyyy-MM-dd hh:mm:ss.S can never match 30-Jun-12. You need dd-MMM-yy. See also SimpleDateFormat javadoc.
    2. When parsing day/month names, make sure that you specify the right locale matching the language used in the day/month names. Your system default locale seems to be not English at all.

    So this should do:

    SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
    Date date = sdf.parse("30-Jun-12");
    

    A third problem, which is actually more a design matter, you should not be using java.sql.Timestamp (and other SQL specific date types) anywhere in your model/business layer (you originally tagged the question JSF). You should only use it in your data layer. Use java.util.Date instead and convert only at exactly the moment you need to persist it in the DB like so

    preparedStatement.setTimestamp(1, new Timestamp(date.getTime()));
    
    0 讨论(0)
  • 2021-01-22 12:09

    If you develop web application and you run it in browser, try change preferable language settings in browser settings. Different countries have different date format, there is major difference between European Countries and United States, so try in your browser (eg. Firefox):

    Tools => Options => Content => Choose preferable language 
    

    Make sure when you make your application focused in browser that language bar on the taskbar shows desirable langage.

    If web application is not case: Check regional setting and preferable language settings.

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