zoukankan      html  css  js  c++  java
  • Calendar

     1 /*
     2  * Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
     3  * 
     4  * public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
     5  */
     6 public class CalendarDemo {
     7     public static void main(String[] args) {
     8         // 其日历字段已由当前日期和时间初始化:
     9         Calendar rightNow = Calendar.getInstance(); // 子类对象
    10 
    11         // 获取年
    12         int year = rightNow.get(Calendar.YEAR);
    13         // 获取月
    14         int month = rightNow.get(Calendar.MONTH);
    15         // 获取日
    16         int date = rightNow.get(Calendar.DATE);
    17 
    18         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    19     }
    20 }

    ================================

     1 /*
     2  * public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
     3  * public final void set(int year,int month,int date):设置当前日历的年月日
     4  */
     5 public class CalendarDemo {
     6     public static void main(String[] args) {
     7         // 获取当前的日历时间
     8         Calendar c = Calendar.getInstance();
     9 
    10         // 获取年
    11         int year = c.get(Calendar.YEAR);
    12         // 获取月
    13         int month = c.get(Calendar.MONTH);
    14         // 获取日
    15         int date = c.get(Calendar.DATE);
    16         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    17 
    18         // // 三年前的今天
    19         // c.add(Calendar.YEAR, -3);
    20         // // 获取年
    21         // year = c.get(Calendar.YEAR);
    22         // // 获取月
    23         // month = c.get(Calendar.MONTH);
    24         // // 获取日
    25         // date = c.get(Calendar.DATE);
    26         // System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    27 
    28         // 5年后的10天前
    29         c.add(Calendar.YEAR, 5);
    30         c.add(Calendar.DATE, -10);
    31         // 获取年
    32         year = c.get(Calendar.YEAR);
    33         // 获取月
    34         month = c.get(Calendar.MONTH);
    35         // 获取日
    36         date = c.get(Calendar.DATE);
    37         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    38         System.out.println("--------------");
    39 
    40         c.set(2011, 11, 11);
    41         // 获取年
    42         year = c.get(Calendar.YEAR);
    43         // 获取月
    44         month = c.get(Calendar.MONTH);
    45         // 获取日
    46         date = c.get(Calendar.DATE);
    47         System.out.println(year + "年" + (month + 1) + "月" + date + "日");
    48     }
    49 }

    ================================

     1 /*
     2  * 获取任意一年的二月有多少天
     3  * 
     4  * 分析:
     5  *         A:键盘录入任意的年份
     6  *         B:设置日历对象的年月日
     7  *             年就是A输入的数据
     8  *             月是2
     9  *             日是1
    10  *         C:把时间往前推一天,就是2月的最后一天
    11  *         D:获取这一天输出即可
    12  */
    13 public class CalendarTest {
    14     public static void main(String[] args) {
    15         // 键盘录入任意的年份
    16         Scanner sc = new Scanner(System.in);
    17         System.out.println("请输入年份:");
    18         int year = sc.nextInt();
    19 
    20         // 设置日历对象的年月日
    21         Calendar c = Calendar.getInstance();
    22         c.set(year, 2, 1); // 其实是这一年的3月1日
    23         // 把时间往前推一天,就是2月的最后一天
    24         c.add(Calendar.DATE, -1);
    25 
    26         // 获取这一天输出即可
    27         System.out.println(c.get(Calendar.DATE));
    28     }
    29 }
  • 相关阅读:
    [Bullet3]创建世界(场景)及常见函数
    [erlang]supervisor(监控树)的重启策略
    [game]十字链表的AOI算法实现
    [翻译][erlang]cowboy handler模块的使用
    数据挖掘算法系列目录
    Spark原理分析目录
    Spark实战系列目录
    2019年读书书单
    Hadoop源码解读系列目录
    分布式架构系列目录
  • 原文地址:https://www.cnblogs.com/jiangjianzhu/p/5807207.html
Copyright © 2011-2022 走看看