How to get Century from date in Java

前端 未结 5 934
孤独总比滥情好
孤独总比滥情好 2021-01-04 20:41

How to get current Century from a date in Java?

For example the date \"06/03/2011\" according to format \"MM/dd/yyyy\". How can I get curre

5条回答
  •  一向
    一向 (楼主)
    2021-01-04 21:22

    Split it by the slahes, get the first two symbols of the third element in the resulting array, Integer.parseInt it and add 1, that is:

    String arr = myDate.split("/");
    String shortYear = myDate[2].substring(0, 2);
    int century = Integer.parseInt(shortYear) + 1;
    
    (not sure about the substring() syntax off the top of my head)

提交回复
热议问题