如何在Java中从日期获取Century [英] How to get Century from date in Java
本文介绍了如何在Java中从日期获取Century的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从Java中的日期获取当前Century?
How to get current Century from a date in Java?
例如日期 2011年6月3日
根据格式 MM / dd / yyyy
。我如何使用 SimpleDateFormat
从这个日期开始获取当前世纪?
For example the date "06/03/2011"
according to format "MM/dd/yyyy"
. How can I get current century from this date using SimpleDateFormat
?
推荐答案
Harry Lime发表的内容稍有变化。他的逻辑并不完全正确。 1901年是20世纪,而1900年是19世纪。
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;
}
}
这篇关于如何在Java中从日期获取Century的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文