Chinaunix首页 | 论坛 | 博客
  • 博客访问: 243602
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-05-01 19:42:55



点击(此处)折叠或打开

  1. /*
  2.  *
  3.  * Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
  4.  *
  5.  * public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
  6.  *
  7.  *
  8.  * public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
  9.  * public final void set(int year,int month,int date):设置当前日历的年月日
  10.  */
  11. public class CalendarDemo {
  12.     public static void main(String[] args) {
  13.         
  14.         
  15.         
  16.         // 获取当前的日历时间
  17.         Calendar c = Calendar.getInstance();

  18.         // 获取年
  19.         int year = c.get(Calendar.YEAR);
  20.         // 获取月
  21.         int month = c.get(Calendar.MONTH);
  22.         // 获取日
  23.         int date = c.get(Calendar.DATE);
  24.         System.out.println(year + "年" + (month + 1) + "月" + date + "日");

  25.         // // 三年前的今天
  26.         // c.add(Calendar.YEAR, -3);
  27.         // // 获取年
  28.         // year = c.get(Calendar.YEAR);
  29.         // // 获取月
  30.         // month = c.get(Calendar.MONTH);
  31.         // // 获取日
  32.         // date = c.get(Calendar.DATE);
  33.         // System.out.println(year + "年" + (month + 1) + "月" + date + "日");

  34.         // 5年后的10天前
  35.         c.add(Calendar.YEAR, 5);
  36.         c.add(Calendar.DATE, -10);
  37.         // 获取年
  38.         year = c.get(Calendar.YEAR);
  39.         // 获取月
  40.         month = c.get(Calendar.MONTH);
  41.         // 获取日
  42.         date = c.get(Calendar.DATE);
  43.         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
  44.         System.out.println("--------------");

  45.         c.set(2011, 11, 11);
  46.         // 获取年
  47.         year = c.get(Calendar.YEAR);
  48.         // 获取月
  49.         month = c.get(Calendar.MONTH);
  50.         // 获取日
  51.         date = c.get(Calendar.DATE);
  52.         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
  53.     }
  54. }

阅读(580) | 评论(0) | 转发(0) |
0

上一篇:Date的概述和常用方法

下一篇:单链表

给主人留下些什么吧!~~