How to format a date String into desirable Date format

前端 未结 5 1076
南旧
南旧 2021-01-22 17:11

I was trying to format a string into date.

For this I have written a code:-

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateF         


        
5条回答
  •  醉话见心
    2021-01-22 17:33

    The same class you use for output formatting of dates can also be used to parse dates on input.

    • SimpleDateFormat reference

    To use your example, to parse the sample date:

    String dt = "2010-10-22";
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
    System.out.println(dateFormatter.parse(dt));
    

    The fields that are not specified (ie. hour, minutes, etc) will be 0. So your same code can be used to format the date on output.

提交回复
热议问题