获取当前时间后的一周的时间和星期

获取一周七天

/**-------------第一天---------------------------------------------------*/
Date today1 = new Date();
// 获取当前日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String day1 = sdf.format(today1);

System.out.println("获取当前日期: " + day1);

// 获取当前时间的星期(当前星期在获取的时候必须-1)
Calendar c1 = Calendar.getInstance();
c1.setTime(today1);
int weekday1 = c1.get(Calendar.DAY_OF_WEEK);
weekday1 = weekday1 - 1;

System.out.println("获取当前时间的星期: " + weekday1);

/**-------------第二天---------------------------------------------------*/
//获取当前日期两天后的日期
Date today2 = new Date();
//SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar c2 = Calendar.getInstance();
c2.setTime(today2);
c2.add(Calendar.DAY_OF_MONTH, +1);
today2 = c2.getTime();

System.out.println("获取两天后的日期: " + sdf.format(today2));

//获取两天后的星期
c2.setTime(today2);
int weekday2 = c2.get(Calendar.DAY_OF_WEEK);
weekday2 = weekday2 - 1;

System.out.println("获取两天后的星期: " + weekday2);

/**-------------第三天---------------------------------------------------*/
//获取当前日期三天后的日期
Date today3 = new Date();
//SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar c3 = Calendar.getInstance();
c3.setTime(today3);
c3.add(Calendar.DAY_OF_MONTH, +2);
today3 = c3.getTime();

System.out.println("获取三天后的日期: " + sdf.format(today3));

//获取三天后的星期
c3.setTime(today3);
int weekday3 = c3.get(Calendar.DAY_OF_WEEK);
weekday3 = weekday3 - 1;

System.out.println("获取三天后的星期: " + weekday3);

/**-------------第四天---------------------------------------------------*/
//获取当前日期四天后的日期
Date today4 = new Date();
//SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar c4 = Calendar.getInstance();
c4.setTime(today4);
c4.add(Calendar.DAY_OF_MONTH, +3);
today4 = c4.getTime();

System.out.println("获取四天后的日期: " + sdf.format(today4));

//获取四天后的星期
c4.setTime(today4);
int weekday4 = c4.get(Calendar.DAY_OF_WEEK);
weekday4 = weekday4 - 1;

System.out.println("获取四天后的星期: " + weekday4);

/**-------------第五天---------------------------------------------------*/
//获取当前日期五天后的日期
Date today5 = new Date();
//SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar c5 = Calendar.getInstance();
c5.setTime(today5);
c5.add(Calendar.DAY_OF_MONTH, +4);
today5 = c5.getTime();

System.out.println("获取五天后的日期: " + sdf.format(today5));

//获取五天后的星期
c5.setTime(today5);
int weekday5 = c5.get(Calendar.DAY_OF_WEEK);
weekday5 = weekday5 - 1;

System.out.println("获取五天后的星期: " + weekday5);

/**-------------第六天---------------------------------------------------*/
//获取当前日期六天后的日期
Date today6 = new Date();
//SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar c6 = Calendar.getInstance();
c6.setTime(today6);
c6.add(Calendar.DAY_OF_MONTH, +5);
today6 = c6.getTime();

System.out.println("获取六天后的日期: " + sdf.format(today6));

//获取六天后的星期
c6.setTime(today6);
int weekday6 = c6.get(Calendar.DAY_OF_WEEK);
weekday6 = weekday6 - 1;

System.out.println("获取六天后的星期: " + weekday6);

/**-------------第七天---------------------------------------------------*/
//获取当前日期七天后的日期
Date today7 = new Date();
//SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

Calendar c7 = Calendar.getInstance();
c7.setTime(today7);
c7.add(Calendar.DAY_OF_MONTH, +6);
today7 = c7.getTime();

System.out.println("获取七天后的日期: " + sdf.format(today7));

//获取七天后的星期
c7.setTime(today7);
int weekday7 = c7.get(Calendar.DAY_OF_WEEK);
weekday7 = weekday7 - 1;

System.out.println("获取七天后的星期: " + weekday7);

你可能感兴趣的:(Java,解决方案)