Parse Date from String in this format : dd/MM/yyyy [to dd/MM/yyyy]

后端 未结 4 887
死守一世寂寞
死守一世寂寞 2020-12-03 23:45

I thinking what is the best way in Java to parse the String with this format dd/MM/yyyy [to dd/MM/yyyy]. The string with the [] are optional and dd stand for the 2 digit pre

4条回答
  •  自闭症患者
    2020-12-04 00:17

    you can do something like this -

    String input = "02/08/2010 [to 31/12/2010]";
        java.text.DateFormat format = new java.text.SimpleDateFormat("dd/MM/yyyy");
        java.util.Date d = null;
        try {
            d = format.parse(input.split(" ")[0]);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(d) ;
    

    if string with the [] is not present still input.split(" ")[0] will return the first string only.

提交回复
热议问题