原文详解:java的Calendar日历对象处理
原文链接: http://licocom.com/archives/1185
java日历数据处理对象中,我们经常会遇见,将现有的日历对象进行操作。
例:将日历减去当前月份-1,具体方法如下:
public String getDate() {
String resultStr = new String();
Calendar calendar = Calendar.getInstance();//创建日历对象
calendar.setTime(new Date());//设置当前时间
calendar.add(Calendar.MONTH, -1);//当前日历对象的月份向后推算1
calendar.getTime();//获取当前日历对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//将日期格式化
resultStr = sdf.format(calendar.getTime());//将获取的时间,传入并获取
return resultStr;//返回处理的数据参数
}
将日历如何处理,按照对应的月份或年份进行修改,如上便可。
面向需求开发,记录学习之路。