java中关于日期的处理

1、某个时间与当前系统时间相差几小时(包括0.5等小数点)

  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm");

  Date rwks = simpleDateFormat.parse(“2019-10-16 18:00”);

  Date now = simpleDateFormat.parse(new Date().toLocaleString());

  long diff = rwks.getTime() - now.getTime() ;

  float hours = diff / (float)(1000 * 60 * 60);

2、取当前系统时间的日期格式:

  Calendar dayc1 = new GregorianCalendar();

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

  Date daystart = sdf.parse(new Date().toLocaleString());

  dayc1.setTime(daystart);

  1>取系统当天的日期:

  int towd = dayc1.get(Calendar.DATE);

  2>取系统当天加1天的日期

  dayc1.add(Calendar.DAY_OF_YEAR, 1);

  xcsj = sdf.format(dayc1.getTime());

  3>取系统当天是周几

  int w=dayc1.get(Calendar.DAY_OF_WEEK)-1;  (此方法是从周日开始)

  if(w==0) w=7;

  4>取系统当天是第几周

  int week1 = dayc1.get(Calendar.WEEK_OF_MONTH);

3、当前时间减30分钟、1小时、2小时

  public String getxcsj(String kssj,String min){

    String xcsj = "";

    try {

      SimpleDateFormat sim = new SimpleDateFormat("HH:mm");

      Date rwkssjd = sim.parse(kssj);

      if("0.5".equals(min)){

        Date txTime = new Date(rwkssjd.getTime() - 30*60*1000);

        xcsj = sim.format(txTime);

      }else if("1".equals(min)){

        Date txTime = new Date(rwkssjd.getTime() - 60*60*1000);

        xcsj = sim.format(txTime);

      }else if("2".equals(min)){

        Date txTime = new Date(rwkssjd.getTime() - 2*60*60*1000);

        xcsj = sim.format(txTime);

      }

    } catch (Exception e) {

      log.error("获取下次提醒时间错误"+e.toString(), e);

    }

    return xcsj;

  }

 

posted @ 2019-10-15 15:31  堇色安年  阅读(337)  评论(0编辑  收藏  举报