有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

     Calendar thatDay = Calendar.getInstance();
      thatDay.set(Calendar.DAY_OF_MONTH,25);
      thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
      thatDay.set(Calendar.YEAR, 1985);
    
      Calendar today = Calendar.getInstance();
    
      long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis
    

    这是一个近似值

    long days = diff / (24 * 60 * 60 * 1000);
    

    要从字符串中解析日期,可以使用

      String strThatDay = "1985/08/25";
      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
      Date d = null;
      try {
       d = formatter.parse(strThatDay);//catch exception
      } catch (ParseException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      } 
    
    
      Calendar thatDay = Calendar.getInstance();
      thatDay.setTime(d); //rest is the same....