package liu.Calendar;
import java.util.Calendar;
public class Demon_Calendar {
/**
* @param args
* 通过Calendar.getInstance()方法创建对象
* 对于CalendarClass的理解,字段相当于成员变量,set方法相当于给成员变量赋值,get方法是获取字段值,add方法是修改字段值/
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar ca = Calendar.getInstance();//通getinstance方法创建Calendar对象
System.out.println(ca);
//ca.add(Calendar.MONTH, +1);//add修改字段值+为加,-为减
//ca.set(Calendar.YEAR,3);//set设置年字段
//ca.set(2000, 10, 5);//设置年,月日字段.2000年10月5日
System.out.print(ca.get(Calendar.YEAR));//get获取字段值
System.out.print(ca.get(Calendar.MONTH));//0-11,0,代表january1月
System.out.print(ca.get(Calendar.DAY_OF_MONTH));//sunday-saturday:,周日代表第一天
System.out.print(ca.get(Calendar.DAY_OF_WEEK));
System.out.println();
System.out.println(ca.get(Calendar.YEAR)+"年"+ca.get(Calendar.MONTH)+"月"+ca.get(Calendar.DAY_OF_MONTH)+"日"
+"星期"+ca.get(Calendar.DAY_OF_WEEK));//系统表示当前日期格式
System.out.println(ca.get(Calendar.YEAR)+"年"+getnum((ca.get(Calendar.MONTH)+1))+"月"+getnum(ca.get(Calendar.DAY_OF_MONTH))+"日"
+getweek(ca.get(Calendar.DAY_OF_WEEK)));//修改后的日期格式
}
//写方法让小于10的数字统前面统一加0
public static String getnum(int num){
if (num<10) {
return "0"+num;
}else{
return ""+num;
}
}
//写方法显示正常的星期排序
public static String getweek(int week) {
String[] arry = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
// TODO Auto-generated method stub
return arry[week];
}
}
|
|