JAVA日历API Calendar的使用总结

近日项目中用到了日历框架Calendar,遂做个简单用法记录,更复杂的用法请查看官方文档:https://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html

几个常用的参数(懒,就从idea截图了):

1,YEAR 可以获取或设置日历中'年'这个的属性,SECOND可以获取或设置秒这个属性,例如获取当前秒就可以get(SECOND)。

1,WEEK_OF_MONTH 意为每月的第几周,DAY_OF_YEAR意为每年的第几天,其他的以此类推。

2,有的人可能疑惑DATE和DAY_OF_MONTH有什么区别,可以看这两个静态成员变量的值是一样的,都为5,所以他们的作用也是一样的。同理WEEK_OF_MONTH和WEDNESDAY作用也是一样的,所以不要用WEDNESDAY获取今天是周几

官方api对DATE解释:Field number for get and set indicating the day of the month. This is a synonym for DAY_OF_MONTH. The first day of the month has value 1

构造方法

Calendar(TimeZone zone, Locale aLocale) 因为不同地区的日期可能会有区别,所以可以获取制定时区和地狱的日历,以此衍生了四个获取实例的方法。

几个常用的方法

add(int field, int amount) 

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

给日历中的某个属性 例如 '天' 加或减多少天,示例在下方 afterDays 方法。

get(int field) 
          Returns the value of the given calendar field.

返回指定的属性(例如小时)的值,示例在下方 printlnDate 方法

set(int field, int value) 
          Sets the given calendar field to the given value.

给日历中的field属性(例如月份)设置一个值,示例在下方 testSet 方法。

Calendar测试类:

public class DateUtils {

    public static void main(String[] args) {
        System.out.println("printlnDate方法开始打印 ==》》");
        printlnDate();
        System.out.println("testSet方法开始打印 ==》》");
        testSet();
        System.out.println("afterDays方法开始打印 ==》》");
        afterDays(new Date(), 8);
    }

    /**
     * 测试一些参数的作用
     */
    public static void printlnDate() {
        Calendar cal = Calendar.getInstance();

        int year = cal.get(Calendar.YEAR);
        // 月份是从0开始的所以要加1
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        int secend = cal.get(Calendar.SECOND);

        System.out.println("当期时间: " + cal.getTime());
        System.out.println("年份: " + year);
        System.out.println("月份: " + month);
        System.out.println("日  : " + day);
        System.out.println("秒  :" + secend);

        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);
        int woy = cal.get(Calendar.WEEK_OF_YEAR);

        // 星期日为一周的第一天输出为 1,星期一输出为 2,以此类推
        System.out.println("一周的第几天: " + dow);
        System.out.println("一月中的第几天: " + dom);
        System.out.println("一年的第几天: " + doy);
        System.out.println("一年中的第几周 " + woy);
    }

    /**
     * 获取date之后多少天的日期
     *
     * @param date
     * @param after
     * @return
     */
    public static void afterDays(Date date, Integer after) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, after);
        System.out.println("之前的日期为:" + date);
        System.out.println(after + "天之后的日期为:" + cal.getTime());
    }

    /**
     * set方法
     */
    public static void testSet() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        System.out.println("之前的日期:" + cal.getTime());
        cal.set(Calendar.DAY_OF_MONTH, 23);
        System.out.println("set之后的日期:" + cal.getTime());
    }
}

打印结果:

printlnDate方法开始打印 ==》》
当期时间: Thu Aug 02 11:26:18 CST 2018
年份: 2018
月份: 8
日  : 2
秒  :18
一周的第几天: 5
一月中的第几天: 2
一年的第几天: 214
一年中的第几周 31


testSet方法开始打印 ==》》
之前的日期:Thu Aug 02 11:26:18 CST 2018
set之后的日期:Thu Aug 23 11:26:18 CST 2018


afterDays方法开始打印 ==》》
之前的日期为:Thu Aug 02 11:26:18 CST 2018
8天之后的日期为:Fri Aug 10 11:26:18 CST 2018

猜你喜欢

转载自blog.csdn.net/fanxing1964/article/details/81353850