java让Android查找日期并计算从现在到现在的天数 6 月,2 周 Questions & Answers 1152 我在这方面是新手。 但我想知道,比如2013年7月的第一个星期天,然后安卓应该计算从现在到那时的天数。 非常感谢你的帮助
# 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....
# 1 楼答案
这是一个近似值
要从字符串中解析日期,可以使用