/**
*
*/
package com.zhiyou.O;
import java.util.Calendar;
/**
* @author Administrator
*
*/
public class CalendarTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// getInstance() 单例类单一实例
// Calendar抽象类
// 抽象类不能实例化
Calendar rightNow = Calendar.getInstance();
// 获得年月日 时分秒都可以
System.out.println(rightNow.get(Calendar.YEAR));
// 月份定义是从0开始的
System.out.println(rightNow.get(Calendar.MONTH)+ 1);
System.out.println(rightNow.get(Calendar.DAY_OF_MONTH));
// Calendar HOUR MINITE SECOND
// 老外的星期日是1
// 星期一是2
// 星期二是3
// 以此类推
System.out.println(rightNow.get(Calendar.DAY_OF_WEEK));
// set设置
rightNow.set(Calendar.YEAR, 2038);
rightNow.set(Calendar.MONTH, 2);
rightNow.set(Calendar.DAY_OF_MONTH, 1);
// get 获得
int weekVale = rightNow.get(Calendar.DAY_OF_WEEK);
System.out.println(weekVale);
// 封装一个方法,给定具体的日期,返回星期几?
}
}
封装一个方法,给定具体的日期,返回星期几?
/**
*
*/
package com.zhiyou.O;
import java.util.Calendar;
/**
* @author Administrator
*
*/
public class CalendarClassTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CalendarClassTest demo1 = new CalendarClassTest();
demo1.print(2025, 12, 1);
}
public void print(int year, int month, int day) {
Calendar rightNow = Calendar.getInstance();
rightNow.set(Calendar.YEAR, year);
rightNow.set(Calendar.MONTH, month);
rightNow.set(Calendar.DAY_OF_MONTH, day);
int weekValue = rightNow.get(Calendar.DAY_OF_WEEK);
System.out.println(weekValue);
}
}