Some summary of Calendar usage

Calendar is an abstract class mainly used to manipulate calendar time fields. In the Java .util.Calendar package, Calendar is basically used in development. Here is a summary of the frequently used ones.

Commonly used methods

abstract void add(int field, int amount)

This method adds or subtracts the specified amount of time to a given calendar field, based on calendar rules.

boolean after(Object obj)

This method returns whether the current calendar represents a time after the time represented by the specified Object.

boolean before(Object when)

This method returns whether the current calendar represents a time before the time represented by the specified Object.

void clear()

This method sets all calendar field values ​​and time values ​​(milliseconds from epoch to offset) of this Calendar of undefined.

Object clone()

This method creates and returns a copy of this object.

int compareTo(Calendar anotherCalendar)

This method compares the time values ​​(from epoch to millisecond offset) represented by two Calendar objects.

boolean equals(Object obj)

This method makes the calendar compare the specified object.

int get(int field)

This method returns the value of the given calendar field.

int getActualMaximum(int field)

This method returns the maximum value the specified calendar field may have, given this Calendar time value.

int getActualMinimum(int field)

This method returns the smallest possible value for the specified calendar field, given this Calendar time value.

static Calendar getInstance()

This method obtains a calendar using the default time zone and locale.

static Calendar getInstance(Locale aLocale)

This method gets a calendar using the default time zone and the specified locale.

static Calendar getInstance(TimeZone zone)

This method gets a calendar using the specified time zone and default locale.

static Calendar getInstance(TimeZone zone, Locale aLocale)

This method gets a calendar specifying the time zone and locale.

abstract int getMaximum(int field)

This method returns the maximum value of the given calendar field for this Calendar instance.

abstract int getMinimum(int field)

This method returns the minimum value of the given calendar field for this Calendar instance.

Date getTime()

This method returns a Date object representing this Calendar's time value (from epoch to "millisecond offset").

long getTimeInMillis()

This method returns the time value of this Calendar in milliseconds.

abstract void roll(int field, boolean up)

 This method adds or subtracts (up/down) a cell at a given time field without changing the larger field.

void roll(int field, int amount)

This method adds the specified (signed) amount to the specified calendar field without changing the larger field.

void set(int field, int value)

This method sets the given calendar field to the given value.

void set(int year, int month, int date)

This method sets the values ​​of the calendar fields YEAR, MONTH, and DAY_OF_MONTH..

void set(int year, int month, int date, int hourOfDay, int minute)

This method is set to the calendar field values ​​YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

void set(int year, int month, int date, int hourOfDay, int minute, int second)

This method sets the field values ​​YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.


package com.gin.xjh.newcalendar;

import android.util.Log;

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

/**
 * Name: CalendarUtils
 * Author: xieganag
 * Email:
 * Date: 2018-04-23 15:25
 */

public class CalendarUtils {
    /**
     * get the date in a few days
     * number positive number is later negative number indicates the previous 0 indicates today
     */
    public static void getOntherDay(int number) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + number);
        Log.d("日期是", calendar.get(Calendar.DAY_OF_MONTH) + "");
    }

    /* Calculate the number of days in a month */
    public static void maxDay(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);//The default January is 0
        int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * Calculate the number of days between two arbitrary times
     */
    public static int getDaysBetween(Calendar day1, Calendar day2) {
        if (day1.after(day2)) {
            Calendar swap = day1;
            day1 = day2;
            day2 = swap;
        }
        int days = day2.get(Calendar.DAY_OF_YEAR) - day1.get(Calendar.DAY_OF_YEAR);
        int y2 = day2.get(Calendar.YEAR);
        if (day1.get(Calendar.YEAR) != y2) {
            day1 = (Calendar) day1.clone();
            do {
                days += day1.getActualMaximum(Calendar.DAY_OF_YEAR);//Get the actual number of days in the current year
                day1.add(Calendar.YEAR, 1);
            } while (day1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

    /**
     * The roll() method is the same as the add() method, but the roll() method loops in this month. For example, if the 31st of July adds five days, the result of the add() method is the 5th of August;
     * The roll() method is on July 5th, the roll() method is used less, and the add() method is generally used more. Test the roll() method:
     */
    public void roll(int year, int month, int day, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        Date date = calendar.getTime();
        calendar.roll(Calendar.DATE, num);
        date = calendar.getTime();
        System.out.println(df.format(date));
    }
}

By the way, if you need to convert to get the exact time, you can use the following specific time period to modify it yourself

final static String simpleDateString = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(simpleDateString);
    simpleDateFormat.format(calendar.getTime())

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324871298&siteId=291194637