According to the recent statistics of the time used in class Calendar

Recently encountered some requirements are based on the number of the corresponding date statistics on how statistical left to the next, this article introduces the Calendar tools used, because for a long time do not forget a little, so be make a note of it, there may also need to look at the
main Date, Calendar class in java time, but most of the methods Date outdated, so the main use or Calendar, and more convenient to use

About Calendar

Calendar class is an abstract class that provides a specific instant and the conversion between a group such as a calendar field YEAR, MONTH, DAY_OF_MONTH, HOUR some other method, and for manipulating the calendar fields (e.g., date of obtaining the next week) provided some method. Instantly available millisecond value to indicate that it is from the epoch (ie GMT 00 January 1, 1970 are: 00: 00.000, Gregorian calendar) offset

Because it is an abstract class, instances can not be obtained directly, so that by the following method

Calendar calendar = Calendar.getInstance();

Underlying source code is provided with a static method to get the object

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

application

Check the day [get]

static int YEAR Field number for get and set indicating the year

static int MONTH Field number for get and set indicating the month.

static int DAY_OF_MONTH Field number for get and set indicating the day of the month.

Field digital static int DAY_OF_WEEK get and set indicating the day of the week.

Field digital static int DAY_OF_YEAR get and set indicating the number of days in the current year.

get (int field) Returns the value of a given calendar field.
The above int field- given calendar field values of static variables

Calendar calendar = Calendar.getInstance();
        int y=calendar.get(Calendar.YEAR);
        System.out.println(y+"年");
        //月份是从0到11
        int m=calendar.get(Calendar.MONTH);
        System.out.println((m+1)+"月");
        int d=calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(d+"日");
        //此处为魔法值,本文写于周日,周日对应魔法值为1,周一为2,以此类推
        //源码:public final static int SUNDAY = 1;
        int wd=calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println("星期魔法值"+wd);
        int yd=calendar.get(Calendar.DAY_OF_YEAR);
        System.out.println("一年中第"+yd+"天");
2019121日
星期魔法值1
一年中第335

Setting day

In my project as an example of
my project framework is springboot combination jpa, query between two periods of time, such as one day from 00:00:00 to 23:59:59 the end of the beginning

Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
``
Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);       
 
甚至还可以直接set想想要的日期,很方便了

```java
calendar.set(2019,12,1);

About Vital Statistics for the current month forward 12 months, I will write the next article

Published 25 original articles · won praise 22 · views 3647

Guess you like

Origin blog.csdn.net/weixin_42443419/article/details/103337247
Recommended