转载

Java Calendar类使用总结

平时在浏览一些网站时,有些网站会显示出当前时间,如现在是xx年xx月xx日 xx时xx分xx秒,在实际的开发过程中,也会涉及到日期和时间的计算,Java中提供了一个专门的类Calendar来处理日期与时间。

接下来就讲解下Calendar类的使用场景及使用方法。

1.获取Calendar实例

首先,我们看下Calendar类的定义

public abstract class Calendar implements Serializable, Cloneable, Comparable<Calendar>

因为被定义为abstract,所以我们无法通过new来创建Calendar类的实例,不过Calendar类提供了一个类方法getInstance()来返回Calendar的实例。

public static Calendar getInstance()
{
    return createCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT));
}

由此,获取Calendar实例的代码如下所示:

Calendar calendar = Calendar.getInstance();

2.获取当前时间

package com.zwwhnly.springbootdemo;

import java.util.Calendar;

public class CalendarDemo {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);
        // 月份的下标是从0开始的,即0~11分别代表1~12月,因此需要+1
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);  // 24小时制
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        System.out.println("现在是:" + year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒");
    }
}

运行结果:

现在是:2019年2月21日15时36分38秒

注意:月份的下标是从0开始的,即0~11分别代表1~12月

3.设置时间

假设我们现在需要将时间设置为2019-02-21 23:59:59

3.1(一起设置):

Calendar calendar = Calendar.getInstance();
calendar.set(2019, Calendar.FEBRUARY, 21, 23, 59, 59);

System.out.println(calendar.getTime());

3.2(分别设置):

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2019);
calendar.set(Calendar.MONTH, Calendar.FEBRUARY);
calendar.set(Calendar.DAY_OF_MONTH, 21);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);

System.out.println(calendar.getTime());

3.1和3.2的运行结果都如下所示:

Thu Feb 21 23:59:59 CST 2019

4.时间计算

4.1增加秒

我们在3.1的基础上增加1秒,那么时间应该是2019-02-22 00:00:00

public static void main(String[] args) {
     Calendar calendar = Calendar.getInstance();
     calendar.set(2019, Calendar.FEBRUARY, 21, 23, 59, 59);
     calendar.add(Calendar.SECOND, 1);
     System.out.println(calendar.getTime());
}

运行结果:

Fri Feb 22 00:00:00 CST 2019

4.2增加月

首先我们将时间设置为2019-01-31,然后先增加1个月,再增加2个月

Calendar calendar = Calendar.getInstance();

calendar.set(2019, Calendar.JANUARY, 31);
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH, 1);
System.out.println(calendar.getTime());
calendar.add(Calendar.MONTH, 2);
System.out.println(calendar.getTime());

运行结果:

Thu Jan 31 15:58:03 CST 2019

Thu Feb 28 15:58:03 CST 2019

Sun Apr 28 15:58:03 CST 2019

注意事项:当所在的月份没有那个日期时,如2月份没有31号,返回的是所在月的最后一天(2月28号)

5.扩展:获取某月的第一天和最后一天

package com.zwwhnly.springbootdemo;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CalendarDemo {
    public static void main(String[] args) {
        System.out.println(getFirstDayOfMonth(2019, 2));
        System.out.println(getLastDayOfMonth(2019, 2));

        System.out.println(getFirstDayOfMonth(2019, 3));
        System.out.println(getLastDayOfMonth(2019, 3));
    }

    public static String getLastDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DATE));

        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    public static String getFirstDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getMinimum(Calendar.DATE));

        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }
}

运行结果:

2019-02-01 2019-02-28 2019-03-01 2019-03-31

Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-03/157515.htm

原文  https://www.linuxidc.com/Linux/2019-03/157515.htm
正文到此结束
Loading...