Parsing a date in Java?

前端 未结 7 1596
清酒与你
清酒与你 2020-12-04 03:15

I have dates in text format of the form dd-mmm-yy or d-mmm-y, where months are abbreviations in letters (for example, 4-Nov-09 or 12-Dec-05, etc...) I would like to parse it

7条回答
  •  生来不讨喜
    2020-12-04 03:35

    You can do it with java.text.SimpleDateFormat. Click the link, you'll see all patterns.

    For 1-2 digit days you can use the d pattern. For 3-character month abbreviations you can use the MMM pattern. For 2 digit years you can use the yy pattern.

    So the following should do:

    String dateString = "4-Nov-09";
    SimpleDateFormat sdf = new SimpleDateFormat("d-MMM-yy");
    Date date = sdf.parse(dateString);
    

提交回复
热议问题