核心:使用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写的很好。

最新文章

  1. Python标准模块--os
  2. C# ArrayList的用法
  3. ZwQuerySystemInformation 安全使用心得 Delphi 版
  4. python模块httplib的使用
  5. C# 对象深度拷贝
  6. GTD时间管理(1)---捕获搜集
  7. dedecms 忘记后台密码
  8. (转)adb shell am 的用法
  9. JSP基础笔记
  10. Web控件
  11. 射频识别技术漫谈(9)——动物标签HDX
  12. OpenStack优先
  13. Spring4.14 事务异常 NoUniqueBeanDefinitionException: No qualifying bean of type [....PlatformTransactionManager]
  14. linux安装redis-3.0.7
  15. Flex中通过RadioButton进行切换
  16. 如何知道局域网内哪些ip被占用----工具法Free IP Scanner
  17. R语言reads.table 自动将字符串变成了逻辑值
  18. powerdesigner导出sql时报错 Generation aborted due to errors detected during the verification of the model.
  19. 查看设备uuid的命令-blkid
  20. Python - 列表与字符串的互相转换

热门文章

  1. HDU 4405 Aeroplane chess (概率DP求期望)
  2. hdu4289 Control --- 最小割,拆点
  3. window.open()方法(弹出层)
  4. AndroidUI组件之ActionBar
  5. Mongodb---记一次事故故障
  6. bzoj3047:Freda的传呼机&&bzoj2125: 最短路
  7. codeforces 931E Logical Expression dp
  8. nodejs安装express
  9. php的string编码类型
  10. css3 选择器 权重问题 (第二部分)