View Code
package test1;
import java.util.Calendar;
public class Date {
int year;
int month;
int day;
int week;
int hour;
int minute;
int second;
// 跟类同名的构造方法
public Date(int year, int month, int day, int week, int hour, int minute,
int second) {
this.year = year;
this.month = month;
this.day = day;
this.week = week;
this.hour = hour;
this.minute = minute;
this.second = second;
}
public static void main(String[] args) {
Date date = date(2011, 12, 26);
System.out.println(date.year + "年" + date.month + "月" + date.day + "日 "
+ date.hour + "点" + date.minute + "分" + date.second + "秒 周"
+ date.week);
}
public static Date date(int year, int month, int day) {
Calendar cal = Calendar.getInstance();
// 月份要注意减1
cal.set(year, month - 1, day);
year = cal.get(Calendar.YEAR);
month = cal.get(Calendar.MONTH);
day = cal.get(Calendar.DAY_OF_MONTH);
// 星期注意减一
int week = cal.get(Calendar.DAY_OF_WEEK) - 1;
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minute = cal.get(Calendar.MINUTE);
int second = cal.get(Calendar.SECOND);
Date date = new Date(year, month, day, week, hour, minute, second);
return date;
}
}