Java Calendar class and method overview

Class Calendar

  • Calendar Class Overview
Calendar class is an abstract class that specific instant in a set of such YEAR , MONTH , DAY_OF_MONTH is , HOUR to translate some other calendar field method, and for manipulating the calendar fields (e.g., date of obtaining the next week) provided some method .
  • Member method
public static Calendar getInstance ()
public int GET ( int Field,): Returns the value of a given calendar field. Each class calendar calendar fields are static member variables, and is of type int.
public void add ( you field, you Amount )
public final void set( int year,int month,int date)

 

Code testing demo:

package cn.itcast_01;

import java.util.Calendar;

/*
 * Calendar:它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
 * 
 * public int get(int field):返回给定日历字段的值。日历类中的每个日历字段都是静态的成员变量,并且是int类型。
 */
public class CalendarDemo {
	public static void main(String[] args) {
		// 其日历字段已由当前日期和时间初始化:
		Calendar rightNow = Calendar.getInstance(); // 子类对象(多态思想)

		// 获取年
		int year = rightNow.get(Calendar.YEAR);
		// 获取月
		int month = rightNow.get(Calendar.MONTH);
		// 获取日
		int date = rightNow.get(Calendar.DATE);

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

/*
 * abstract class Person { public static Person getPerson() { return new
 * Student(); } }
 * 
 * class Student extends Person {
 * 
 * }
 */

 

Date class earlier study, we were able to format and create a date object, but how can we set up and access specific parts of it date data, such as hours, days, or minutes? How do we add in these parts of the date or Less to value it? The answer is to use Calendar category.

Calendar functions like a lot of powerful than the Date class, but also in the implementation of the Date class than more complicated.

Calendar class is an abstract class , an object that implements a specific sub-class in actual use, the creation of objects is transparent to the programmer only needs to be created to use the getInstance method.

Create a Calendar object that represents the current date of the system

Calendar c = Calendar.getInstance();//默认是当前日期

Creating a Calendar object specified date

Use Calendar class represents a particular time, you must first create an object Calendar, and then set the date parameters of the object to complete.

//创建一个代表2009年6月12日的Calendar对象
Calendar c1 = Calendar.getInstance();
c1.set(2009, 6 - 1, 12);

Calendar Object class field type

Calendar class with the following constants have different meanings, much like this idea are actually used in the jdk

 

constant description
Calendar.YEAR years
Calendar.MONTH month
Calendar.DATE date
Calendar.DAY_OF_MONTH Date, and fields exactly the same meanings as described above
Calendar.HOUR 12 hour h
Calendar.HOUR_OF_DAY 24-hour clock hours
Calendar.MINUTE minute
Calendar.SECOND second
Calendar.DAY_OF_WEEK Day of the week
 

 

Member method:

  • public void the Add ( int Field, int AMOUNT): the given calendar field and the corresponding time, to operate the current calendar.
  • Final void the SET public ( int year, int month The, int DATE): set the current calendar date

Calendar information provided class object

  • Set Set
Calendar c1 = Calendar.getInstance();

transfer:

public final void set(int year,int month,int date)
c1.set(2009, 6, 12);//把Calendar对象c1的年月日分别设这为:2009、6、12

Field type using

If only setting a field, such as the value date, the method can use the following set:

public void set(int field,int value)

C1 object represents the date is set to 10, all other values ​​are recalculated

c1.set(Calendar.DATE,10);

C1 object represents the year is set to 2008, all other values ​​will be recalculated

c1.set(Calendar.YEAR,2008);
  • Add Setting
Calendar c1 = Calendar.getInstance();

把c1对象的日期加上10,也就是c1也就表示为10天后的日期,其它所有的数值会被重新计算

c1.add(Calendar.DATE, 10);

把c1对象的日期减去10,也就是c1也就表示为10天前的日期,其它所有的数值会被重新计算

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

代码测试如下:

package cn.wen_02;

import java.util.Calendar;

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 + "日");

		
                //add
                // // 5年前
		// c.add(Calendar.YEAR, -5);
		// // 获取年
		// 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("--------------");

		
                //set
                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 + "日");
	}
}

 

小练习:获取任意一年的二月有多少天
 
分析:
        A:键盘录入任意的年份
        B:设置日历对象的年月日
              年就是A输入的数据

              月是2
              日是1
        C:把时间往前推一天,就是2月的最后一天
        D:获取这一天输出即可
 

package cn.itcast_03;

import java.util.Calendar;
import java.util.Scanner;

public class CalendarTest {
	public static void main(String[] args) {
		// 键盘录入任意的年份
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = sc.nextInt();

		// 设置日历对象的年月日
		Calendar c = Calendar.getInstance();
		c.set(year, 2, 1); // 其实是这一年的3月1日
		// 把时间往前推一天,就是2月的最后一天
		c.add(Calendar.DATE, -1);

		// 获取这一天输出即可
		System.out.println(c.get(Calendar.DATE));
	}
}

 

发布了91 篇原创文章 · 获赞 16 · 访问量 1178

Guess you like

Origin blog.csdn.net/hewenqing1/article/details/103844494