s-day01--Calendar类概述
This commit is contained in:
@@ -0,0 +1,49 @@
|
|||||||
|
package com.inmind.calendar03;
|
||||||
|
|
||||||
|
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
|
||||||
|
----------------------------------
|
||||||
|
abstract void add(int field, int amount) 根据日历的规则,将指定的时间量添加或减去给定的日历字段。
|
||||||
|
参数一:要修改的字段固定值
|
||||||
|
参数二:要增加或减少的值
|
||||||
|
*/
|
||||||
|
public class Demo07 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//获取一个日历对象
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
System.out.println(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));*/
|
||||||
|
showTime(calendar);
|
||||||
|
//想将2026年改为2025年
|
||||||
|
calendar.set(Calendar.YEAR,2025);
|
||||||
|
calendar.set(Calendar.MONTH,3);
|
||||||
|
showTime(calendar);
|
||||||
|
//我要在当前年份上+2年,月份上-14月
|
||||||
|
calendar.add(Calendar.YEAR,2);
|
||||||
|
calendar.add(Calendar.MONTH,-12);
|
||||||
|
showTime(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void showTime(Calendar calendar) {
|
||||||
|
System.out.println(calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DAY_OF_MONTH)+"日");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user