JAVA-获取当天时间和下一天时间,以一天时间变化

Posted

tags:

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

 一、显示当天

  today_date_1=new Date();
  today_date_2=new Date();
  today_cal_1=Calendar.getInstance();//获取当前时间
  today_cal_2=Calendar.getInstance(); 
  today_time_2=null;
  
  today_time_1=formatter_1.format(today_date_1);
  StringBuffer str_todayFirst=new StringBuffer().append(today_time_1).append("  00:00:00");
  today_time_1=str_todayFirst.toString();
  text1.setText(today_time_1);
  
  today_cal_1.add(Calendar.DATE,+1);
  today_date_2=today_cal_1.getTime();
  today_time_2=formatter_1.format(today_date_2); 
  StringBuffer str_todayLast=new StringBuffer().append(today_time_2).append("  00:00:00");
  today_time_2=str_todayLast.toString();
  text2.setText(today_time_2);

二、以天为单位变化

1、向前推一天

   today_cal_1.add(Calendar.DATE,-2);
   today_date_1=today_cal_1.getTime();
   today_time_1=formatter_1.format(today_date_1); 
   StringBuffer str_todayFirst=new StringBuffer().append(today_time_1).append("  00:00:00");
   today_time_1=str_todayFirst.toString();
   text1.setText(today_time_1);   
   
   today_cal_1.add(Calendar.DATE,+1);
   today_date_2=today_cal_1.getTime();
   today_time_2=formatter_1.format(today_date_2); 
   StringBuffer str_todayLast=new StringBuffer().append(today_time_2).append("  00:00:00");
   today_time_2=str_todayLast.toString();
   text2.setText(today_time_2);

2、向后推一天

    today_cal_1.add(Calendar.DATE,0);
   today_date_1=today_cal_1.getTime();
   today_time_1=formatter_1.format(today_date_1); 
   StringBuffer str_todayFirst=new StringBuffer().append(today_time_1).append("  00:00:00");
   today_time_1=str_todayFirst.toString();
   text1.setText(today_time_1);
   
   today_cal_1.add(Calendar.DATE,+1);
   today_date_2=today_cal_1.getTime();
   today_time_2=formatter_1.format(today_date_2); 
   StringBuffer str_todayLast=new StringBuffer().append(today_time_2).append("  00:00:00");
   today_time_2=str_todayLast.toString();
   text2.setText(today_time_2);

以上是关于JAVA-获取当天时间和下一天时间,以一天时间变化的主要内容,如果未能解决你的问题,请参考以下文章

java获取指定月份中每天时间列表

java获取指定月份中每天时间列表

jquery 获取当前时间的前7天时间

获取今天时间并且按照规定格式返回

(干货)java中如何根据一个时间获取属于本年那一周,本周的开始时间以及最后一天时间。并且设置起始时间为周6.结束时间为周5

获取当前时间的前一天或后一天时间