package com.igenetech.manage.utils.cache;
import java.util.Calendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyUtil {
public static Date add(Date date, int calendarField, int amount) {
if (date == null) throw new IllegalArgumentException("日期不能为空");
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(calendarField, amount);
return c.getTime();
}
public static Date parse(String src, String pattern) {
if (pattern == null) throw new IllegalArgumentException("日期格式不能为空");
if (src == null || "".equals(src)) return null;
try {
return new SimpleDateFormat(pattern).parse(src);
} catch (ParseException ex) {
throw new IllegalArgumentException("日期格式转换出错,src=" + src + ",pattern=" + pattern);
}
}
/**
* 把Date类型日期按照指定的格式转换成字符串类型的日期
*
* @return
* @ param src
* @ param pattern
*/
public static String format(Date src, String pattern) {
if (pattern == null) throw new IllegalArgumentException("日期格式不能为空");
if (src == null) return null;
return new SimpleDateFormat(pattern).format(src);
}
/**
* 取得当前日期时间字串.格式"yyyy-MM-dd".
*
* @return 字符串:
*/
public static String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dt = sdf.format(new Date());
return dt;
}
/**
* 取得当前日期所属年份.格式"yyyy-MM-dd".
*
* @return 字符串:
*/
public static String getYear() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dt = sdf.format(new Date());
return dt.substring(0, 4);
}
/**
* 解析相应字符串到Date对象,模式: "yyyy-MM-dd HH:mm:ss"
*
* @return java.utils.Date
* @ param param datetime 字符串
*/
public static Date parse(String param) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = sdf.parse(param);
} catch (ParseException ex) {
}
return date;
}
/**
* 通过一个date字符串取出年份
*
* @return int 年
* @throws NumberFormatException
* @ param date 如:2003-10-15 不能使用中文的"-"
*/
public static int getYear(String date) throws NumberFormatException {
String m = date.substring(0, 4);
return Integer.parseInt(m);
}
/**
* 通过一个date字符串取出月份
*
* @return int 月
* @throws NumberFormatException
* @ param date 如:2003-10-15 不能使用中文的"-"
*/
public static int getMonth(String date) throws NumberFormatException {
String m = date.substring(date.indexOf("-") + 1, date.lastIndexOf("-"));
return Integer.parseInt(m);
}
/**
* 通过一个date字符串取出季度
*
* @return int 季度数
* @throws NumberFormatException
* @ param date 如:2010-12-01
*/
public static int getQuarter(String date) throws NumberFormatException {
int q = (getMonth(date) - 1) / 3 + 1;
return q;
}
/**
* 功能:得到当前季度季初 格式为:yyyy-MM-dd (eg: 2007-10-01)<br>
*
* @return String
* @author pure
*/
public static String getQuarterBegin(String strDate) {
int q = getQuarter(strDate);
int y = getYear(strDate);
String dateString = "";
if (q == 1) {
dateString = y + "-" + "01" + "-" + "01";
}
if (q == 2) {
dateString = y + "-" + "04" + "-" + "01";
}
if (q == 3) {
dateString = y + "-" + "07" + "-" + "01";
}
if (q == 4) {
dateString = y + "-" + "10" + "-" + "01";
}
return dateString;
}
/**
* 功能:得到当前季度季末 格式为:yyyy-MM-dd (eg: 2007-12-31)<br>
*
* @return String
* @author pure
*/
public static String getQuarterEnd(String strDate) {
String dateString = "";
int q = getQuarter(strDate);
int y = getYear(strDate);
if (q == 1) {
dateString = y + "-" + "03" + "-" + "31";
}
if (q == 2) {
dateString = y + "-" + "06" + "-" + "30";
}
if (q == 3) {
dateString = y + "-" + "09" + "-" + "30";
}
if (q == 4) {
dateString = y + "-" + "12" + "-" + "31";
}
return dateString;
}
/**
* 根据不带天的时间间隔,将一段时间分成若干个符合要求的时间段
*
* @param startDate,开始时间,如2018-02
* @param endDate,结束时间,如2019-03
* @param inteval:时间间隔,1,2,3等,其中intevalUnit为quarter(季度)时,此值不起作用
* @param intevalUnit:间隔单位,接受值值域:year、quarter、month
* @return
*/
public static List<Map<String, String>> getIntervalDateList(String startDate, String endDate, Integer inteval, String intevalUnit) {
Date end = null;
Date start = null;
try {
start = new SimpleDateFormat("yyyy-MM").parse(startDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
end = new SimpleDateFormat("yyyy-MM").parse(endDate);
} catch (ParseException e) {
e.printStackTrace();
}
ArrayList<Map<String, String>> list = new ArrayList<>();
if ("month".equals(intevalUnit)) {
while (start.getTime() <= end.getTime()) {
HashMap<String, String> map = new HashMap<>();
Calendar cStart = Calendar.getInstance();
cStart.setTime(start);
map.put("startDate", new SimpleDateFormat("yyyy-MM").format(cStart.getTime()).concat("-01"));
map.put("trendDesc", new SimpleDateFormat("yyyy年MM月").format(cStart.getTime()));//区间描述
cStart.add(Calendar.MONTH, inteval);
if (cStart.getTime().getTime() > end.getTime()) {
Calendar temp = Calendar.getInstance();
temp.setTime(end);
temp.set(Calendar.DAY_OF_MONTH, 1);// 设为本月第一天
temp.add(Calendar.MONTH, 1);// 月份加一
temp.add(Calendar.DATE, -1);// 天数加 -1 = 上一个月的最后一天
map.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(temp.getTime()));
if (inteval > 1) {//如果间隔大于1,格式特殊调整
StringBuilder sb = new StringBuilder();
sb.append(map.get("trendDesc"));
sb.append("至");
sb.append(new SimpleDateFormat("yyyy年MM月").format(temp.getTime()));
map.put("trendDesc", sb.toString());
}
} else {
Calendar temp = Calendar.getInstance();
temp.setTime(cStart.getTime());
temp.set(Calendar.DAY_OF_MONTH, 1);// 设为本月第一天
// temp.add(Calendar.MONTH, 1);// 月份加一
temp.add(Calendar.DATE, -1);// 天数加 -1 = 上一个月的最后一天
map.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(temp.getTime()));
if (inteval > 1) {//如果间隔大于1,格式特殊调整
StringBuilder sb = new StringBuilder();
sb.append(map.get("trendDesc"));
sb.append("至");
sb.append(new SimpleDateFormat("yyyy年MM月").format(temp.getTime()));
map.put("trendDesc", sb.toString());
}
}
list.add(map);
start = cStart.getTime();
}
}
if ("quarter".equals(intevalUnit)) {
startDate = startDate.concat("-01");
endDate = endDate.concat("-01");
String quarterBegin = getQuarterBegin(startDate);
String quarterEnd = getQuarterEnd(endDate);
try {
start = new SimpleDateFormat("yyyy-MM-dd").parse(quarterBegin);
} catch (ParseException e) {
e.printStackTrace();
}
try {
end = new SimpleDateFormat("yyyy-MM-dd").parse(quarterEnd);
} catch (ParseException e) {
e.printStackTrace();
}
while (start.getTime() <= end.getTime()) {
HashMap<String, String> map = new HashMap<>();
Calendar cStart = Calendar.getInstance();
cStart.setTime(start);
map.put("startDate", new SimpleDateFormat("yyyy-MM-dd").format(cStart.getTime()));
map.put("trendDesc", new SimpleDateFormat("yyyy年").format(cStart.getTime()) + "第" + getQuarter(new SimpleDateFormat("yyyy-MM-dd").format(cStart.getTime())) + "季度");
cStart.add(Calendar.MONTH, 3);
if (cStart.getTime().getTime() > end.getTime()) {
map.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(end.getTime()));
} else {
Calendar temp = Calendar.getInstance();
temp.setTime(cStart.getTime());
temp.set(Calendar.DAY_OF_MONTH, 1);// 设为本月第一天
temp.add(Calendar.DATE, -1);// 天数加 -1 = 上一个月的最后一天
map.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(temp.getTime()));
}
System.out.println(new SimpleDateFormat("yyyy年MM月dd日").format(cStart.getTime()));
list.add(map);
start = cStart.getTime();
}
}
if ("year".equals(intevalUnit)) {
while (start.getTime() <= end.getTime()) {
HashMap<String, String> map = new HashMap<>();
Calendar cStart = Calendar.getInstance();
cStart.setTime(start);
map.put("startDate", new SimpleDateFormat("yyyy").format(cStart.getTime()).concat("-01-01"));
map.put("trendDesc", new SimpleDateFormat("yyyy年").format(cStart.getTime()));//区间描述
cStart.add(Calendar.YEAR, inteval);
if (cStart.getTime().getTime() > end.getTime()) {
Calendar temp = Calendar.getInstance();
temp.setTime(end);
temp.set(Calendar.DAY_OF_YEAR, 1);// 设为本年第一天
temp.add(Calendar.YEAR, 1);// 月份加一
temp.add(Calendar.DATE, -1);// 天数加 -1 = 上一年的最后一天
map.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(temp.getTime()));
if (inteval > 1) {//如果间隔大于1,格式特殊调整
StringBuilder sb = new StringBuilder();
sb.append(map.get("trendDesc"));
sb.append("至");
sb.append(new SimpleDateFormat("yyyy年").format(temp.getTime()));
map.put("trendDesc", sb.toString());
}
} else {
Calendar temp = Calendar.getInstance();
temp.setTime(cStart.getTime());
temp.set(Calendar.DAY_OF_YEAR, 1);// 设为本年第一天
// temp.add(Calendar.MONTH, 1);// 月份加一
temp.add(Calendar.DATE, -1);// 天数加 -1 = 上一年的最后一天
map.put("endDate", new SimpleDateFormat("yyyy-MM-dd").format(temp.getTime()));
if (inteval > 1) {//如果间隔大于1,格式特殊调整
StringBuilder sb = new StringBuilder();
sb.append(map.get("trendDesc"));
sb.append("至");
sb.append(new SimpleDateFormat("yyyy年").format(temp.getTime()));
map.put("trendDesc", sb.toString());
}
}
list.add(map);
start = cStart.getTime();
}
}
return list;
}
public static void main(String[] args) throws Exception {
System.out.println(getIntervalDateList("2012-11", "2018-12", 1, "year"));
}
}