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
Calendar c = Calendar.getInstance();//返回的是子类对象
2.Member method
public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。 public final void set(int year,int month,int date):设置当前日历的年月日
(Video tutorial recommendation: java video tutorial)
Code implementation:
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 + "日"); } }
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.