zoukankan      html  css  js  c++  java
  • Calendar时间类的一些用法

    Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEARMONTHDAY_OF_MONTHHOUR日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法

    package test;
    
    import java.util.Calendar;
    
    public class CalendarDemo {
    
        public static void main(String[] args) {
            Calendar calendar = Calendar.getInstance();
    
            int year = 2016;
    
            showDays(year);
        }
    
        public static void showDays(int year) {
            Calendar calendar = Calendar.getInstance();
            // 设置年,2月,11日
            calendar.set(year, 2, 11);
            // 这里是将11日减去一天,也可以加天数
            calendar.add(calendar.DAY_OF_MONTH, -1);
    
            showDate(calendar);
    
        }
    
        private static void showDate(Calendar calendar) {
    
            int year = calendar.get(Calendar.YEAR);
            // 这里需要注意:MONTH代表月份
            // 月份的起始值不是1,而是0
            // 所以想要设置5月,就应该设置成4而不是5
            int month = calendar.get(Calendar.MONTH);
            int day = calendar.get(Calendar.DAY_OF_MONTH);
            int week = calendar.get(Calendar.DAY_OF_WEEK);
    
            System.out
                    .println(year + "年" + month + "月" + day + "日" + getWeek(week));
    
        }
    
        public static String getWeek(int i) {
            // 国际日期是以星期日为每周第一天
            // 0角标是null,是想让让星期日的角标为1
            String[] weeks = { "", "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
    
            return weeks[i];
        }
    
    }
  • 相关阅读:
    HDU1251 字典树 这题亮点在于是我自己写的字典树
    POJ3253 哈夫曼树+小根堆 【自己实现】
    poj3083 深搜
    用c语言的感觉
    poj1321 深搜
    POJ 2488 深搜
    HDU2037 今年暑假不AC 贪心
    hdu1247 Hat’s Words 字符串模拟
    Thrift框架具体使用
    如何使用Rose将类图转化为java代码
  • 原文地址:https://www.cnblogs.com/fifiyong/p/6080252.html
Copyright © 2011-2022 走看看