Java--以周作为时间变化

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java--以周作为时间变化相关的知识,希望对你有一定的参考价值。

 一、获取本周

 weekFirst_date=new Date();
  weekLast_date=new Date();
  weekFirst_calendar=Calendar.getInstance();
  weekLast_calendar=Calendar.getInstance();
  weekFirst_time=null;
  weekLast_time=null;
  
  weekFirst_calendar.setFirstDayOfWeek(Calendar.MONDAY);
  weekFirst_calendar.add(Calendar.WEEK_OF_MONTH,0);
  weekFirst_calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
  weekFirst_date=weekFirst_calendar.getTime();
  weekFirst_time=formatter_1.format(weekFirst_date);
  StringBuffer weekFirst_str=new StringBuffer().append(weekFirst_time).append("  00:00:00");
  weekFirst_time=weekFirst_str.toString();
  prev_time.setText(weekFirst_time);
  
  weekLast_calendar.set(weekFirst_calendar.DAY_OF_MONTH,weekFirst_calendar.get(weekFirst_calendar.DAY_OF_MONTH)+7);  
  weekLast_date=weekLast_calendar.getTime();
  weekLast_time=formatter_1.format(weekLast_date);
  StringBuffer weekLast_str=new StringBuffer().append(weekLast_time).append("  00:00:00");
  weekLast_time=weekLast_str.toString();
  next_time.setText(weekLast_time);

二、以周为单位变化

1、向前推一周

week_cal_1.set(Calendar.DAY_OF_MONTH,week_cal_1.get(Calendar.DAY_OF_MONTH)-7);
   week_date_1=week_cal_1.getTime();
   week_time_1=formatter_1.format(week_date_1);
   StringBuffer str_weekFirst=new StringBuffer().append(week_time_1).append("  00:00:00");
   week_time_1=str_weekFirst.toString();
   text1.setText(week_time_1);
   
   week_cal_2.set(Calendar.DAY_OF_MONTH,week_cal_2.get(Calendar.DAY_OF_MONTH)-7);
   week_date_2=week_cal_2.getTime();
   week_time_2=formatter_1.format(week_date_2);
   StringBuffer str_weekLast=new StringBuffer().append(week_time_2).append("  00:00:00");
   week_time_2=str_weekLast.toString();
   text2.setText(week_time_2);

2、向后推一周

week_cal_1.set(Calendar.DAY_OF_MONTH,week_cal_1.get(Calendar.DAY_OF_MONTH)+7);
   week_date_1=week_cal_1.getTime();
   week_time_1=formatter_1.format(week_date_1);
   StringBuffer str_weekFirst=new StringBuffer().append(week_time_1).append("  00:00:00");
   week_time_1=str_weekFirst.toString();
   text1.setText(week_time_1);
   
   week_cal_2.set(Calendar.DAY_OF_MONTH,week_cal_2.get(Calendar.DAY_OF_MONTH)+7);
   week_date_2=week_cal_2.getTime();
   week_time_2=formatter_1.format(week_date_2);
   StringBuffer str_weekLast=new StringBuffer().append(week_time_2).append("  00:00:00");
   week_time_2=str_weekLast.toString();
   text2.setText(week_time_2);

以上是关于Java--以周作为时间变化的主要内容,如果未能解决你的问题,请参考以下文章

以周、月、季度和年的形式获取日期之间的差异

4星|《OKR工作法》:关注公司的真正目标,以周为单位做计划和考核

mysql怎根据时间获取是星期几

mysql怎根据时间获取是星期几

java数组作为参数传入函数怎么让他不变化

R,随时间变化作为因子。如何找到最接近某个时间的值?