Parse any date in Java

后端 未结 6 800
时光取名叫无心
时光取名叫无心 2020-11-22 07:31

I know this question is asked quite a bit, and obviously you can\'t parse any arbitrary date. However, I find that the python-dateutil library is able to parse every date I

6条回答
  •  不知归路
    2020-11-22 08:22

    I have no idea about this parsing how to do in python. In java we can do like this

    SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
      java.util.Date normalDate = null;
      java.sql.Date sqlDate = null;
      normalDate = sdf1.parse(date);
      sqlDate = new java.sql.Date(normalDate.getTime());
      System.out.println(sqlDate);
    

    i think like java some predefined functions will be there in python. You can follow this method. This methods parse the String date to Sql Date (dd-MM-yyyy);

    import java.text.SimpleDateFormat;
    import java.text.ParseException;
    public class HelloWorld{
         public static void main(String []args){
            String date ="26-12-2019";
             SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy");
            java.util.Date normalDate = null;
            java.sql.Date sqlDate = null;
            if( !date.isEmpty()) {
                try {
                    normalDate = sdf1.parse(date);
                    sqlDate = new java.sql.Date(normalDate.getTime());
                    System.out.println(sqlDate);
                } catch (ParseException e) {
                }
            }
         }
    } 
    

    execute this!

提交回复
热议问题