1. 效果
2. 工具类
获取===》 时间戳是自 1970 年 1 月 1 日(08:00:00 GMT)至当前时间的总秒数java
System.currentTimeMillis();

package ipi.common.utils;工具 import java.text.Format; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;spa public class TimeUtils { /** * 1.1 * 将用户传递的时间类型转换为字符串类型 * @Title: getStringDate * @Description: * @param date * @return * @return String * @throws @date 2018年6月4日 上午9:17:26 */ public static String getStringDate(Date date) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = formatter.format(date); return currentTime; } /** * 1.2将标准字符串转化类型的时间 转化为Date类型 * @Title: getStringToDate * @Description: * @param time * @return * @throws ParseException * @return Date * @throws @date 2018年8月8日 下午10:57:37 */ public static Date getStringToDate(String time) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); Date date = sdf.parse(time); return date; } /** *2.1 等到年月日 * @Title: getStringShortDate * @Description: * @param date * @return * @return String * @throws @date 2018年7月6日 下午1:27:15 */ public static String getStringShortDate(Date date) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); String currentTime = formatter.format(date); return currentTime; } /** * 2.2 获得 年月 * @Title: getStringYearMouthDate * @Description: * @param date * @return * @return String * @throws @date 2018年8月12日 上午9:58:49 */ public static String getStringYearMouthDate(Date date) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); String currentTime = formatter.format(date); return currentTime; } /** * 3.计算两个日期相差的年数 * @Title: getYearDisparity * @Description: * @param beginTime * @param endTime * @return * @throws Exception * @return String * @throws @date 2018年7月6日 下午2:20:40 */ public static String getYearDisparity(String beginTime,String endTime) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); Calendar bef = Calendar.getInstance(); Calendar aft = Calendar.getInstance(); bef.setTime(sdf.parse(beginTime)); aft.setTime(sdf.parse(endTime)); int year = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)); System.out.println(Math.abs(year)); return year+""; } /** * 4.计算两个日期相差的月数 * @Title: getMonthsDisparity * @Description: * @param beginTime * @param endTime * @return * @throws Exception * @return String * @throws @date 2018年7月6日 下午2:30:54 */ public static String getMonthsDisparity(String beginTime,String endTime) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM"); String str1 = "2011-02"; String str2 = "2010-01"; Calendar bef = Calendar.getInstance(); Calendar aft = Calendar.getInstance(); bef.setTime(sdf.parse(str1)); aft.setTime(sdf.parse(str2)); int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH); int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12; String months = ( result+month)+""; return months; } /** * 5.计算两个日期相差的天数 * @Title: getMonthsDisparity * @Description: * @param beginTime * @param endTime * @return * @throws Exception * @return String * @throws @date 2018年7月6日 下午2:21:41 */ public static String getdaysDisparity(String beginTime,String endTime) throws Exception { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date2 = format.parse(endTime); Date date = format.parse(beginTime); int days = (int) ((date2.getTime() - date.getTime()) / (1000*3600*24)); return days+""; } /** * 6. 拿到如今时间加上(减去) d天后的时间 d是正数加 负数减去 * @Title: getStringAfterOperationDay * @Description: * @param d * @return * @return String * @throws @date 2018年7月30日 上午11:18:39 */ public static String getStringAfterOperationDay(Integer d) { Format f = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, d);// 昨天-1天 Date yesterday = c.getTime(); String yesterdayBegin = f.format(yesterday).toString(); return yesterdayBegin; } /*** * 7.1 获取距离今天 天 的0点时间 好比2018-07-30 00:00:000 * @Title: getStringAfterOperationBeginDay * @Description: * @param d * @return * @return String * @throws @date 2018年7月30日 下午6:30:04 */ public static String getStringAfterOperationBeginDay(Integer d) { Format f = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, d);// 正数 加 Date yesterday = c.getTime(); String beginTime = f.format(yesterday).toString()+" 00:00:00"; return beginTime; } /** * 7.2 获取距离今天 天 的0点时间 好比2018-07-30 23:59:000 * @Title: getStringAfterOperationEndDay * @Description: * @param d * @return * @return String * @throws @date 2018年7月30日 下午6:33:50 */ public static String getStringAfterOperationEndDay(Integer d) { Format f = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_MONTH, d);// 负数减 Date yesterday = c.getTime(); String endTime = f.format(yesterday).toString()+" 23:59:59"; return endTime; } } .net |