如何获得上一个月和多年的java? [英] How to get previous month and years in java?

查看:124
本文介绍了如何获得上一个月和多年的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 10th oct 2012, result should be Month = 9 and year = 2012, If today is 10th jan 2013, 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);

然后您可以调用日历上的1.5.0 / docs / api / java / util / Calendar.html#get%28int%29> getter 以获取正确的字段



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆