两个时间点计算相隔几年,几个月,几天-java
Posted ggband
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了两个时间点计算相隔几年,几个月,几天-java相关的知识,希望对你有一定的参考价值。
本文采用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"; } }
以上是关于两个时间点计算相隔几年,几个月,几天-java的主要内容,如果未能解决你的问题,请参考以下文章