package com.book.lite;
import sun.misc.ClassFileTransformer;
import java.util.Calendar;
/**
* @author zhangyu
* @date 2021年08月22日 4:22 下午
* java.util.Calender类, 生活中的日历进行对象封装
* 1.abstract Calender类
* 2.使用子类GregorianCalendar类 数据类型是整数类
* 3.获取对象 static Calender getInstance()
* 静态方法返回值是Calender类的对象,因为是私有的,返回的是GregorianCalender类对象
*
* 4.Calender类的get(int)返回值是int
* 5.Calender类的get(int)传入的参数是Calender类的静态方法
*
* 6.修改日历set()
* 7.setTime() 接收Date对象
* 8.setTimeInMillis 接收毫秒值
*
* 9.Calender类的add()方法
* add(int filed,int amount)
* int amount
*
*/
public class CalenderDemo {
public static void main(String[] args) {
//
// Calendar todat = Calendar.getInstance();
// printDate(todat);
setDate();
methon_3();
method_4();
}
public static void printDate(Calendar todat){
// System.out.println(todat);
int year = todat.get(Calendar.YEAR);
int month = todat.get(Calendar.MONTH);
int date = todat.get(Calendar.DATE);
int DateOfweek = todat.get(Calendar.DAY_OF_WEEK);
int hour = todat.get(Calendar.HOUR_OF_DAY);
int minute = todat.get(Calendar.MINUTE);
int second = todat.get(Calendar.SECOND);
// System.out.println(year);
// System.out.println(month+1);
// System.out.println(date);
// System.out.println(DateOfweek-1);
// System.out.println(hour);
// System.out.println(minute);
// System.out.println(second);
System.out.println(year+"年"+getmonth(month+1)+"月"+date+"日"+" "+getweek(DateOfweek)+" "+hour+"点"+minute+"分"+second+"秒");
}
public static String getweek(int week){
String [] str = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
return str[week-1];
}
public static String getmonth(int month){
if (month < 10){
return "0"+month;
}
else {
return month+"";
}
}
public static void setDate(){
Calendar data = Calendar.getInstance();
printDate(data);
data.set(Calendar.YEAR,2020);
data.set(2020,9,23);
printDate(data);
}
public static void methon_3(){
Calendar c = Calendar.getInstance();
long time = 1234567890234L;
c.setTimeInMillis(time);
printDate(c);
}
public static void method_4(){
Calendar c = Calendar.getInstance();
//定义年份
int year = 2008;
//日历定位到2008年,3月1日
c.set(year,2,1);
//向前偏移一天
c.add(Calendar.DAY_OF_MONTH,-1);
//获取偏移后的天数
int date = c.get(Calendar.DAY_OF_MONTH);
System.out.println(date);
}
}
(JAVA)Calender类
最新推荐文章于 2024-08-25 21:47:23 发布