Home Java Javagetting Started Examples of the Calendar class in java

Examples of the Calendar class in java

Jul 23, 2020 pm 05:10 PM
calendar java

Examples of the Calendar class in java

Calendar class, it is an abstract class that encapsulates all calendar field values. The values ​​can be obtained through a unified method based on the different calendar fields passed in.

(Recommended tutorial: java introductory tutorial)

1. Get a calendar object

1

Calendar c = Calendar.getInstance();//返回的是子类对象

Copy after login

2.Member method

1

2

public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。

public final void set(int year,int month,int date):设置当前日历的年月日

Copy after login

(Video tutorial recommendation: java video tutorial)

Code implementation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

import java.util.Calendar;

  

/*

 * public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。

 * public final void set(int year,int month,int date):设置当前日历的年月日

 */

public class CalendarDemo {

    public static void main(String[] args) {

        // 获取当前的日历时间

        Calendar c = Calendar.getInstance();

  

        // 获取年

        int year = c.get(Calendar.YEAR);

        // 获取月

        int month = c.get(Calendar.MONTH);

        // 获取日

        int date = c.get(Calendar.DATE);

        System.out.println(year + "年" + (month + 1) + "月" + date + "日");

  

        // // 三年前的今天

        // c.add(Calendar.YEAR, -3);

        // // 获取年

        // year = c.get(Calendar.YEAR);

        // // 获取月

        // month = c.get(Calendar.MONTH);

        // // 获取日

        // date = c.get(Calendar.DATE);

        // System.out.println(year + "年" + (month + 1) + "月" + date + "日");

  

        // 5年后的10天前

        c.add(Calendar.YEAR, 5);

        c.add(Calendar.DATE, -10);

        // 获取年

        year = c.get(Calendar.YEAR);

        // 获取月

        month = c.get(Calendar.MONTH);

        // 获取日

        date = c.get(Calendar.DATE);

        System.out.println(year + "年" + (month + 1) + "月" + date + "日");

        System.out.println("--------------");

  

        c.set(2011, 11, 11);

        // 获取年

        year = c.get(Calendar.YEAR);

        // 获取月

        month = c.get(Calendar.MONTH);

        // 获取日

        date = c.get(Calendar.DATE);

        System.out.println(year + "年" + (month + 1) + "月" + date + "日");

    }

}

Copy after login

The above is the detailed content of Examples of the Calendar class in java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Square Root in Java

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Perfect Number in Java

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Random Number Generator in Java

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Armstrong Number in Java

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Weka in Java

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Smith Number in Java

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

Java Spring Interview Questions

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Break or return from Java 8 stream forEach?

See all articles