我正在使用以下代码获取当前日期(格式为12/31/1999,即mm / dd / yyyy):
Textview txtViewData; txtViewDate.setText("Today is " + android.text.format.DateFormat.getDateFormat(this).format(new Date()));
我还有另一个日期格式为:2019-08-25(即yyyy / mm / dd),
所以我想找出日期之间的天数差异,如何找到天数上的差异?
(换句话说,我想找到当前日期-yyyy / mm / dd格式化日期之间的差异)
并不是真正可靠的方法,最好使用JodaTime
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....
但是,由于你确定日期格式Integer.parseInt(),因此也可以在子字符串上执行操作以获取其数字值。
Integer.parseInt()