s-day01-Calendar类概述和取值赋值方法
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
package com.inmind.calendar04;
|
||||||
|
|
||||||
|
import java.util.Calendar;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Date类大部分的功能都过时了,那么JDK提供了一个更好用的类辅助Date,Calendar类
|
||||||
|
Calendar类是一个抽象类,必须使用它的子类,它比较特殊,通过静态工具方法,返回一个日历对象
|
||||||
|
|
||||||
|
获取日历对象的方法:
|
||||||
|
static Calendar getInstance() 使用默认时区和区域设置获取日历。
|
||||||
|
---------------------------------------------------
|
||||||
|
Calendar里面的get方法
|
||||||
|
int get(int field) 返回给定日历字段的值。
|
||||||
|
|
||||||
|
---------------------------
|
||||||
|
Calendar里面的set方法
|
||||||
|
void set(int field, int value) 将给定的日历字段设置为给定的值。
|
||||||
|
参数一:日历类固定的值
|
||||||
|
参数二:修改后的值
|
||||||
|
Date类操作时间必须传入毫秒值,日历类提供了直接操作年月日,时分秒的api
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
System.out.println(calendar);//底层调用的toString方法
|
||||||
|
//-----------------获取日历的指定的时间值---------------
|
||||||
|
//int get(int field) 返回给定日历字段的值。
|
||||||
|
printCanlder(calendar);
|
||||||
|
//-----------------|---操作日历的指定的时间值----------------------
|
||||||
|
System.out.println("----------------");
|
||||||
|
//年月日都少1
|
||||||
|
calendar.set(Calendar.YEAR, 2025);
|
||||||
|
calendar.set(Calendar.MONTH,5);
|
||||||
|
calendar.set(Calendar.DAY_OF_MONTH,27);
|
||||||
|
printCanlder(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//定义一个打印日历时间内容的方法
|
||||||
|
public static void printCanlder(Calendar calendar) {
|
||||||
|
System.out.println(calendar.get(Calendar.YEAR));
|
||||||
|
System.out.println(calendar.get(Calendar.MONTH)+1);
|
||||||
|
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
|
||||||
|
System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
|
||||||
|
System.out.println(calendar.get(Calendar.MINUTE));
|
||||||
|
System.out.println(calendar.get(Calendar.SECOND));
|
||||||
|
System.out.println(calendar.get(Calendar.MILLISECOND));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user