import java.util.Calendar;
/**
*
* * Clander的用法。如何取得当前时间一个月后/前的时间。如何取得某个月的最后一天。 如何取得今年的第一个周一。五月的第二个星期日。
* 如何取得今年的第一周(注意国外的日历和国内可能不一样,如何设置一周开始时间)
*/
public class CalendarDemo {
public static void main(String[] args) {
System.out.println("当前时间为:");
System.out.println(getCurrentTime());
System.out.println("当前时间一个月前的时间为:");
System.out.println(getOneMonthBeforeCurrentTime());
System.out.println("当前时间一个月后的时间为:");
System.out.println(getOneMonthAfterCurrentTime());
System.out.println("某月的最后一天时间为:");
System.out.println(getLastDayOfMonth());
System.out.println("今年的第一个周一为:");
System.out.println(getFirstMondayOfYear());
System.out.println("今年五月的第二个星期日为:");
System.out.println(getSecondSunDayOfMay());
System.out.print("第一周从:");
System.out.println(getFirstWeekOfYear());
}
// 获得系统当前时间
public static String getCurrentTime() {
Calendar cal = Calendar.getInstance();
return ""+cal.getTime();
}
// 获得当前时间一个月前的时间
public static String getOneMonthBeforeCurrentTime() {
Calendar cal = Calendar.getInstance();
cal.add(cal.MONTH, -1);
return ""+cal.getTime();
}
// 获得当前时间一个月后的时间
public static String getOneMonthAfterCurrentTime() {
Calendar cal = Calendar.getInstance();
cal.add(cal.MONTH, 1);
return ""+cal.getTime();
}
// 取得某个月的最后一天
public static String getLastDayOfMonth() {
Calendar cal = Calendar.getInstance();
cal.set(2000, 1, 1);// 2000年2月1日
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
// 获得今年的第一个周一
public static String getFirstMondayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), 0, 1);// 今年的一月一日
//如果weekDay =2 是周一
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
int monDay=Calendar.MONDAY;
int sumDay = 0;
if (weekDay == monDay) {
sumDay = 0;
}
else{
sumDay=(7-weekDay+monDay);
}
cal.add(Calendar.DAY_OF_MONTH, sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH);
}
//取得五月的第二个星期日
public static String getSecondSunDayOfMay(){
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), 4, 1);//日期设置为今年的5月1日
// 如果weekDay =1 是周日
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
int sunDay=Calendar.SUNDAY;
int sumDay = 0;
if (weekDay == sunDay) {
sumDay = 7;
}
else{
sumDay=(7-weekDay+sunDay)+7;
}
cal.add(Calendar.DAY_OF_MONTH, sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH);
}
//如何取得今年的第一周
/*
* 每年的第一个周开始的时间是这样计算的:
* 即:如果新年开始的1月1号在周四、周五、周六,则新年的第一周开始的时间是在新年的第二个周日;
* 否则,新年第一周开始的时间是在去年的最后一个周日;
*/
public static String getFirstWeekOfYear(){
Calendar cal=Calendar.getInstance();
// cal.set(cal.get(Calendar.YEAR), 0,1);//设置时间为1月1日
cal.set(2013, 0,1);
int weekDay=cal.get(Calendar.DAY_OF_WEEK);
int sunDay=Calendar.SUNDAY;
int sumDay = 0;
//如果1月1号是周四、周五、周六
if((weekDay==5)||(weekDay==6)||(weekDay==7)){
sumDay=(7-weekDay+sunDay);
cal.add(Calendar.DAY_OF_MONTH, sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";
}
else if(weekDay==1){
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";
}
else {
sumDay=(7-weekDay+sunDay);
cal.add(Calendar.DAY_OF_MONTH, -sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";
}
}
}
public static void printfWeeks(String date) throws Exception {
// String date = "2013-09";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
Date date1 = dateFormat.parse(date);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date1);
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("days:" + days);
int count = 0;
for (int i = 1; i <= days; i++) {
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = dateFormat1.parse(date + "-" + i);
calendar.clear();
calendar.setTime(date2);
int k = new Integer(calendar.get(Calendar.DAY_OF_WEEK));
if (k == 1) {// 若当天是周日
count++;
System.out.println("-----------------------------------");
System.out.println("第" + count + "周");
if (i - 6 <= 1) {
System.out.println("本周开始日期:" + date + "-" + 1);
} else {
System.out.println("本周开始日期:" + date + "-" + (i - 6));
}
System.out.println("本周结束日期:" + date + "-" + i);
System.out.println("-----------------------------------");
}
if (k != 1 && i == days) {// 若是本月最好一天,且不是周日
count++;
System.out.println("-----------------------------------");
System.out.println("第" + count + "周");
System.out.println("本周开始日期:" + date + "-" + (i - k + 2));
System.out.println("本周结束日期:" + date + "-" + i);
System.out.println("-----------------------------------");
}
}
}
/**
*
* * Clander的用法。如何取得当前时间一个月后/前的时间。如何取得某个月的最后一天。 如何取得今年的第一个周一。五月的第二个星期日。
* 如何取得今年的第一周(注意国外的日历和国内可能不一样,如何设置一周开始时间)
*/
public class CalendarDemo {
public static void main(String[] args) {
System.out.println("当前时间为:");
System.out.println(getCurrentTime());
System.out.println("当前时间一个月前的时间为:");
System.out.println(getOneMonthBeforeCurrentTime());
System.out.println("当前时间一个月后的时间为:");
System.out.println(getOneMonthAfterCurrentTime());
System.out.println("某月的最后一天时间为:");
System.out.println(getLastDayOfMonth());
System.out.println("今年的第一个周一为:");
System.out.println(getFirstMondayOfYear());
System.out.println("今年五月的第二个星期日为:");
System.out.println(getSecondSunDayOfMay());
System.out.print("第一周从:");
System.out.println(getFirstWeekOfYear());
}
// 获得系统当前时间
public static String getCurrentTime() {
Calendar cal = Calendar.getInstance();
return ""+cal.getTime();
}
// 获得当前时间一个月前的时间
public static String getOneMonthBeforeCurrentTime() {
Calendar cal = Calendar.getInstance();
cal.add(cal.MONTH, -1);
return ""+cal.getTime();
}
// 获得当前时间一个月后的时间
public static String getOneMonthAfterCurrentTime() {
Calendar cal = Calendar.getInstance();
cal.add(cal.MONTH, 1);
return ""+cal.getTime();
}
// 取得某个月的最后一天
public static String getLastDayOfMonth() {
Calendar cal = Calendar.getInstance();
cal.set(2000, 1, 1);// 2000年2月1日
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
// 获得今年的第一个周一
public static String getFirstMondayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), 0, 1);// 今年的一月一日
//如果weekDay =2 是周一
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
int monDay=Calendar.MONDAY;
int sumDay = 0;
if (weekDay == monDay) {
sumDay = 0;
}
else{
sumDay=(7-weekDay+monDay);
}
cal.add(Calendar.DAY_OF_MONTH, sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH);
}
//取得五月的第二个星期日
public static String getSecondSunDayOfMay(){
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), 4, 1);//日期设置为今年的5月1日
// 如果weekDay =1 是周日
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
int sunDay=Calendar.SUNDAY;
int sumDay = 0;
if (weekDay == sunDay) {
sumDay = 7;
}
else{
sumDay=(7-weekDay+sunDay)+7;
}
cal.add(Calendar.DAY_OF_MONTH, sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH);
}
//如何取得今年的第一周
/*
* 每年的第一个周开始的时间是这样计算的:
* 即:如果新年开始的1月1号在周四、周五、周六,则新年的第一周开始的时间是在新年的第二个周日;
* 否则,新年第一周开始的时间是在去年的最后一个周日;
*/
public static String getFirstWeekOfYear(){
Calendar cal=Calendar.getInstance();
// cal.set(cal.get(Calendar.YEAR), 0,1);//设置时间为1月1日
cal.set(2013, 0,1);
int weekDay=cal.get(Calendar.DAY_OF_WEEK);
int sunDay=Calendar.SUNDAY;
int sumDay = 0;
//如果1月1号是周四、周五、周六
if((weekDay==5)||(weekDay==6)||(weekDay==7)){
sumDay=(7-weekDay+sunDay);
cal.add(Calendar.DAY_OF_MONTH, sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";
}
else if(weekDay==1){
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";
}
else {
sumDay=(7-weekDay+sunDay);
cal.add(Calendar.DAY_OF_MONTH, -sumDay);
return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH)+"开始,到"+cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-"
+ (cal.get(Calendar.DAY_OF_MONTH)+6)+"结束";
}
}
}
public static void printfWeeks(String date) throws Exception {
// String date = "2013-09";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
Date date1 = dateFormat.parse(date);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date1);
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("days:" + days);
int count = 0;
for (int i = 1; i <= days; i++) {
DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
Date date2 = dateFormat1.parse(date + "-" + i);
calendar.clear();
calendar.setTime(date2);
int k = new Integer(calendar.get(Calendar.DAY_OF_WEEK));
if (k == 1) {// 若当天是周日
count++;
System.out.println("-----------------------------------");
System.out.println("第" + count + "周");
if (i - 6 <= 1) {
System.out.println("本周开始日期:" + date + "-" + 1);
} else {
System.out.println("本周开始日期:" + date + "-" + (i - 6));
}
System.out.println("本周结束日期:" + date + "-" + i);
System.out.println("-----------------------------------");
}
if (k != 1 && i == days) {// 若是本月最好一天,且不是周日
count++;
System.out.println("-----------------------------------");
System.out.println("第" + count + "周");
System.out.println("本周开始日期:" + date + "-" + (i - k + 2));
System.out.println("本周结束日期:" + date + "-" + i);
System.out.println("-----------------------------------");
}
}
}