本文采用Calendar 實現 ,當然也可以用java8提供的愉快且方便的時間處理- LocalDate
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DateDaySub {
private static Calendar calS = Calendar.getInstance();
private static Pattern p = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");//定義整則表達式
/**
* 計算剩余時間
*
* @param startDateStr yyyy-MM-dd
* @param endDateStr yyyy-MM-dd
* @return ?年?個月?天
*/
public static String remainDateToString(String startDateStr, String endDateStr) {
java.util.Date startDate = null;
java.util.Date endDate = null;
try {
startDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDateStr);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
try {
endDate = new SimpleDateFormat("yyyy-MM-dd").parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
return "";
}
calS.setTime(startDate);
int startY = calS.get(Calendar.YEAR);
int startM = calS.get(Calendar.MONTH);
int startD = calS.get(Calendar.DATE);
int startDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);
calS.setTime(endDate);
int endY = calS.get(Calendar.YEAR);
int endM = calS.get(Calendar.MONTH);
//處理2011-01-10到2011-01-10,認為服務為一天
int endD = calS.get(Calendar.DATE) + 1;
int endDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);
StringBuilder sBuilder = new StringBuilder();
if (endDate.compareTo(startDate) < 0) {
return sBuilder.append("過期").toString();
}
int lday = endD - startD;
if (lday < 0) {
endM = endM - 1;
lday = startDayOfMonth + lday;
}
//處理天數問題,如:2011-01-01 到 2013-12-31 2年11個月31天 實際上就是3年
if (lday == endDayOfMonth) {
endM = endM + 1;
lday = 0;
}
int mos = (endY - startY) * 12 + (endM - startM);
int lyear = mos / 12;
int lmonth = mos % 12;
if (lyear > 0) {
sBuilder.append(lyear + "年");
}
if (lmonth > 0) {
sBuilder.append(lmonth + "個月");
}
// if(lyear==0)//滿足項目需求 滿一年不顯示天數
if (lday > 0) {
sBuilder.append(lday + "天");
}
return sBuilder.toString();
}
/*
* 轉換 dataAndTime 2013-12-31 23:59:59 到
* date 2013-12-31
*/
public static String getDate(String dateAndTime) {
if (dateAndTime != null && !"".equals(dateAndTime.trim())) {
Matcher m = p.matcher(dateAndTime);
if (m.find()) {
return dateAndTime.subSequence(m.start(), m.end()).toString();
}
}
return "data error";
}
}
