所以我试图减去我提出这个代码的2个日期:
java.text.DateFormat df = new java.text.SimpleDateFormat("hh:mm:ss_yyyy.MM.dd");
java.util.Date date1 = new java.util.Date();
java.util.Date date2 = df.parse("00:00:00_2013.01.01");
long diff = date2.getTime() - date1.getTime();
但问题是差异不正确。
有谁能告诉我我做错了什么?
答案 0 :(得分:6)
评论中的差异样本是错误的。我尝试了你的代码并得到了这个结果:
try {
DateFormat df = new SimpleDateFormat("hh:mm:ss_yyyy.MM.dd");
Date date1 = new java.util.Date();
Date date2 = df.parse("00:00:00_2013.01.01");
long diff = date2.getTime() - date1.getTime();
Log.e("TEST" , date1.getTime() + " - " + date2.getTime() + " - " + diff);
} catch (ParseException e) {
Log.e("TEST", "Exception", e);
}
date1: 1350921506492
date2: 1356994800000
diff: 6073293508
diff / 1000 / 60 / 60 / 24 = 70,292748935
大致检查了70天,直到新年好听。
答案 1 :(得分:0)
public String ConvertDateToReadableDate(String DateTime) {
if (DateTime != null) {
if (!DateTime.equals("")) {
// the input should be in this format 2019-03-08 15:14:29
//if not you have to change the pattern in SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
Date newDate;
Date currentDate = new java.util.Date();
int hour = 0, min = 0, sec = 0;
String dayName = "", dayNum = "", monthName = "", year = "";
long numOfMilliSecondPassed = 0;
float milliSecond = 86400000.0f; // 1 day is 86400000 milliseconds
float numOfDayPass;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
newDate = dateFormat.parse(DateTime); // convert String to date
numOfMilliSecondPassed = currentDate.getTime() - newDate.getTime(); //get the difference in date in millisecond
hour = Integer.parseInt((String) android.text.format.DateFormat.format("hh", newDate));
min = Integer.parseInt((String) android.text.format.DateFormat.format("mm", newDate));
sec = Integer.parseInt((String) android.text.format.DateFormat.format("ss", newDate));
dayName = (String) android.text.format.DateFormat.format("EEEE", newDate);
dayNum = (String) android.text.format.DateFormat.format("dd", newDate);
monthName = (String) android.text.format.DateFormat.format("MMM", newDate);
year = (String) android.text.format.DateFormat.format("yyyy", newDate);
} catch (ParseException e) {
e.printStackTrace();
}
//Convert the milliseconds to days
numOfDayPass = (numOfMilliSecondPassed / milliSecond);
if (numOfDayPass < 1) {
return hour + ":" + min + ":" + sec;
} else if ((numOfDayPass >= 1) && (numOfDayPass < 7)) {
return dayName + " "+ hour + ":" + min + ":" + sec;
} else if ((numOfDayPass >= 7) && (numOfDayPass < 30)) {
int weeks = (int) numOfDayPass / 7;
if(weeks > 1) {
return weeks + " weeks ago";
}else{
return weeks + " week ago";
}
}else if((numOfDayPass >= 30) && (numOfDayPass < 90) ){
int months = (int) numOfDayPass/30;
if(months > 1) {
return months + " months ago";
}else{
return months + " month ago";
}
}else if((numOfDayPass >= 360) && (numOfDayPass < 1080) ){
int years = (int) numOfDayPass/360;
if(years > 1) {
return years + " years ago";
}else{
return years + " year ago";
}
}else{
return dayName + " " + dayNum + " " + monthName + " " + year + " "+
hour + ":" + min + ":" + sec;
}
} else {
return null;
}
} else {
return null;
}
}