From 5eadf208bc38b0002f127217e479aa5353228ec8 Mon Sep 17 00:00:00 2001 From: yujian <yujian> Date: 星期四, 14 二月 2019 17:08:41 +0800 Subject: [PATCH] 迁移 --- fanli-utils/src/main/java/org/yeshi/utils/DateUtil.java | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 112 insertions(+), 0 deletions(-) diff --git a/fanli-utils/src/main/java/org/yeshi/utils/DateUtil.java b/fanli-utils/src/main/java/org/yeshi/utils/DateUtil.java index b0718d9..d625707 100644 --- a/fanli-utils/src/main/java/org/yeshi/utils/DateUtil.java +++ b/fanli-utils/src/main/java/org/yeshi/utils/DateUtil.java @@ -2,8 +2,10 @@ import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; public class DateUtil { @@ -96,5 +98,115 @@ return false; } } + + + /** + * 鎸囧畾鏃ユ湡鍔犱笂澶╂暟鍚庣殑鏃ユ湡 + * + * @param num + * 澧炲姞鐨勫ぉ鏁� + * @param nowDate + * 鍒涘缓鏃堕棿 + * @return + * @throws ParseException + */ + public static String plusDay(int num, String nowDate) throws ParseException { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + Date currdate = format.parse(nowDate); + + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.DATE, num); + + return format.format(ca.getTime()); + } + + /** + * 鎸囧畾鏃ユ湡鍑忓幓澶╂暟鍚庣殑鏃ユ湡 + * + * @param num + * 鍑忓幓鐨勫ぉ鏁� + * @param nowDate + * 鍒涘缓鏃堕棿 + * @return + * @throws ParseException + */ + public static String reduceDay(int num, String nowDate) throws ParseException { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + Date currdate = format.parse(nowDate); + + Calendar ca = Calendar.getInstance(); + ca.setTime(currdate); + ca.add(Calendar.DATE, -num); // 鏃ユ湡鍑� 濡傛灉涓嶅鍑忎細灏嗘湀鍙樺姩 + + return format.format(ca.getTime()); + } + + public static List<String> getDateMonthList(Date beginDate, Date endDate) { + Calendar endCalendar = Calendar.getInstance(); + endCalendar.setTime(endDate); + + Calendar beginCalendar = Calendar.getInstance(); + beginCalendar.setTime(beginDate); + + int month = beginCalendar.get(Calendar.MONTH) - endCalendar.get(Calendar.MONTH); + List<String> list = new ArrayList<String>(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM"); + for (int ii = month; ii < 0; ii++) { + Calendar ca = Calendar.getInstance(); + ca.set(Calendar.DAY_OF_MONTH, 1); + ca.set(Calendar.MONTH, (ca.get(Calendar.MONTH)) + ii + 1); + String format = dateFormat.format(ca.getTime()); + list.add(format); + } + return list; + } + + public static List<String> getDateMonthList(int month, Date date) { + List<String> list = new ArrayList<String>(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM"); + Calendar ca; + for (int ii = -month; ii < 0; ii++) { + ca = Calendar.getInstance(); + ca.setTime(date); + ca.set(Calendar.DAY_OF_MONTH, 1); + ca.set(Calendar.MONTH, (ca.get(Calendar.MONTH)) + ii + 1); + String format = dateFormat.format(ca.getTime()); + list.add(format); + } + return list; + } + + public static int countMonths(String date1, String date2) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + + c1.setTime(sdf.parse(date1)); + c2.setTime(sdf.parse(date2)); + + int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); + + // 锟�?锟斤拷鏃ユ湡鑻ュ皬鏈堢粨鏉熸棩锟�? + if (year < 0) { + year = -year; + return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH); + } + + return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH); + } + + public static int countDay(String date1, String date2) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + + c1.setTime(sdf.parse(date1)); + c2.setTime(sdf.parse(date2)); + int day = ((Long) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / 86400000)).intValue(); + return day; + } } \ No newline at end of file -- Gitblit v1.8.0