How to get Century from date in Java

前端 未结 5 925
孤独总比滥情好
孤独总比滥情好 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:38

    A slight change to what Harry Lime posted. His logic is not entirely correct. Year 1901 would be 20th century, but 1900 would be 19th century.

    public class CenturyYear {
    
        public static void main(String[] args) {
            int test = centuryFromYear(1900);
            System.out.println(test);
    
        }
    
        static int centuryFromYear(int year) {
            if (year % 100 == 0) {
                year = year / 100;
            } else {
                year = (year / 100) + 1;
            }
            return year;
        }
    
    }
    

提交回复
热议问题