java获取当前日期的前一天,前一月和前一年
2024-08-24 05:54:52
核心:使用Calendar的add(int field, int amount)方法
Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
ca.setTime(new Date()); //设置时间为当前时间
ca.add(Calendar.YEAR, -1); //年份减1
Date lastMonth = ca.getTime(); //结果
//求前一月ca.add(Calendar.MONTH, -1),
//前一天ca.add(Calendar.DATE, -1)
同样,类似的,求前一月ca.add(Calendar.MONTH, -1),前一天ca.add(Calendar.DATE, -1)
网上有人说月份这样做是有问题的,比如当前时间是2009-12-31,你在月份上减1变成了2009-12-1”,我试了一下没有出现这样的问题,不过印象中好像确实有遇到过这样的情况,代码是怎么写的已经记不清了
Calendar ca = Calendar.getInstance();//得到一个Calendar的实例
ca.set(2009, 11, 31);//月份是从0开始的,所以11表示12月
Date now = ca.getTime();
ca.add(Calendar.MONTH, -1); //月份减1
Date lastMonth = ca.getTime(); //结果
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sf.format(now));
System.out.println(sf.format(lastMonth));
打印出来的结果是: 2009-12-31
2009-11-30
android代码如何调用系统日历?
Intent intent = new Intent();
intent.setComponent(newComponentName("com.android.calendar", "com.android.calendar.LaunchActivity"));
startActivity(intent);
try {
Intent i = new Intent();
ComponentName cn = null;
if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
cn = new ComponentName("com.android.calendar","com.android.calendar.LaunchActivity");
} else {
cn = new ComponentName("com.google.android.calendar","com.android.calendar.LaunchActivity");
}
i.setComponent(cn);
startActivity(i);
} catch (ActivityNotFoundException e) {
// TODO: handle exception
Log.e("ActivityNotFoundException", e.toString());
}
我还想给它添加点击事件,选择一个日期,然后返回我的代码.后来在网上找到一个自己写的gridview的日历形式,链接如下http://download.csdn.net/detail/onlyonecoder/6574379写的很好。
最新文章
- Python标准模块--os
- C# ArrayList的用法
- ZwQuerySystemInformation 安全使用心得 Delphi 版
- python模块httplib的使用
- C# 对象深度拷贝
- GTD时间管理(1)---捕获搜集
- dedecms 忘记后台密码
- (转)adb shell am 的用法
- JSP基础笔记
- Web控件
- 射频识别技术漫谈(9)——动物标签HDX
- OpenStack优先
- Spring4.14 事务异常 NoUniqueBeanDefinitionException: No qualifying bean of type [....PlatformTransactionManager]
- linux安装redis-3.0.7
- Flex中通过RadioButton进行切换
- 如何知道局域网内哪些ip被占用----工具法Free IP Scanner
- R语言reads.table 自动将字符串变成了逻辑值
- powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model.
- 查看设备uuid的命令-blkid
- Python - 列表与字符串的互相转换