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 current century from this date using SimpleDateFormat?
解决方案
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;
}
}