如何在Java中获得上个月/一年? [英] How to get last month/year in java?
本文介绍了如何在Java中获得上个月/一年?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何用Java找出最后一个月及其年份?
How do I find out the last month and its year in Java?
例如如果今天是2012年10月10日,则结果应为Month = 9
和Year = 2012
.如果今天是2013年1月10日,则结果应为Month = 12
和Year = 2012
.
e.g. If today is Oct. 10 2012, the result should be Month = 9
and Year = 2012
. If today is Jan. 10 2013, the result should be Month = 12
and Year = 2012
.
推荐答案
Your solution is here but instead of addition you need to use subtraction
c.add(Calendar.MONTH, -1);
Then you can call getter on the Calendar
to acquire proper fields
int month = c.get(Calendar.MONTH) + 1; // beware of month indexing from zero
int year = c.get(Calendar.YEAR);
这篇关于如何在Java中获得上个月/一年?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文