このコードをインターネットで検索しましたが、見つけることができませんでした。返事が遅くなってしまいましたが、これは便利なコードです。
public static String getTimeLeft(String date) { // dateFormat = "yyyy-MM-dd"
String[] DateSplit = date.split("-");
int month = Integer.parseInt(DateSplit[1]) - 1, // if month is november then subtract by 1
year = Integer.parseInt(DateSplit[0]), day = Integer
.parseInt(DateSplit[2]), hour = 0, minute = 0, second = 0;
Calendar now = Calendar.getInstance();
int sec = second - Calendar.getInstance().get(Calendar.SECOND), min = minute
- Calendar.getInstance().get(Calendar.MINUTE), hr = hour
- Calendar.getInstance().get(Calendar.HOUR_OF_DAY), dy = day
- Calendar.getInstance().get(Calendar.DATE), mnth = month
- Calendar.getInstance().get(Calendar.MONTH), daysinmnth = 32 - dy;
Calendar end = Calendar.getInstance();
end.set(year, month, day);
if (mnth != 0) {
if (dy != 0) {
if (sec < 0) {
sec = (sec + 60) % 60;
min--;
}
if (min < 0) {
min = (min + 60) % 60;
hr--;
}
if (hr < 0) {
hr = (hr + 24) % 24;
dy--;
}
if (dy < 0) {
dy = (dy + daysinmnth) % daysinmnth;
mnth--;
}
if (mnth < 0) {
mnth = (mnth + 12) % 12;
}
}
}
String hrtext = (hr == 1) ? "hour" : "hours", dytext = (dy == 1) ? "day"
: "days", mnthtext = (mnth == 1) ? "month" : "months";
if (now.after(end)) {
return "";
} else {
String months = "", days = "", hours = "";
months = (mnth > 0) ? mnth + " " + mnthtext : "";
if (mnth <= 0) {
days = (dy > 0) ? dy + " " + dytext : "";
if (dy <= 0) {
hours = (hr > 0) ? hr + " " + hrtext : "";
}
}
//Log.d("DATE", months + " 1 " + days + " 2 " + hours);
return months + days + hours;
}
}