获取中国标准的本周日期

package com.aitang;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Test3 {

public static void main(String[] args) {
String yz_time= getTimeInterval(new Date());//获取本周时间
String array[]=yz_time.split(",");
String mon=array[0];//周一
String tus=array[1]; //周二
String wes=array[2]; //周三
String thur=array[3]; //周四
String fri=array[4]; //周五
String satur=array[5]; //周六
String sun=array[6]; //周日
System.out.println("周一"+mon);
System.out.println("周二"+tus);
System.out.println("周三"+wes);
System.out.println("周四"+thur);
System.out.println("周五"+fri);
System.out.println("周六"+satur);
System.out.println("周日"+sun);


}

private static String getTimeInterval(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
String monDay = sdf.format(cal.getTime());
// System.out.println("所在周星期一的日期:" + monDay);
cal.add(Calendar.DATE, 1);
String tuseDay = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 1);
String wesDay = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 1);
String thurDay = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 1);
String friDay = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 1);
String saturDay = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 1);
String sunDay = sdf.format(cal.getTime());
// System.out.println("所在周星期日的日期:" + imptimeEnd);
return monDay + "," + tuseDay+ "," + wesDay+ "," + thurDay+ "," + friDay+ "," + saturDay+ "," + sunDay;
}

}

posted @ 2018-04-08 16:28  Roger胜  阅读(310)  评论(0编辑  收藏  举报