日期部分相关工具类
package com.jesims.fund.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.Month; import java.util.*; public class DateUtil { private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); public static void main(String[] args) throws ParseException { SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd"); // Date fromDate = format.parse("2020-12-12 00:00:00"); Date fromDate = format.parse("2020-12-12 00:00:00"); String time = "2021-01-01"; String time2 = "2021-12-30"; /* List<String> quarter = getQuarter(time, time2); for (int i = 0; i <quarter.size() ; i++) { String s = quarter.get(i); String year = s.substring(0, 4); String quarter1 = s.substring(5, 7); // System.out.println(year); // System.out.println(quarter1); if (quarter1.equals("Q1")){ // System.out.println(year+"-01-01"); // System.out.println(year+"-02-28"); Calendar c = Calendar.getInstance(); c.setTime(fromDate); c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); String last= format2.format(c.getTime()); System.out.println(last); Calendar c2 = Calendar.getInstance(); // this takes current date c2.setTime(fromDate); c2.set(Calendar.DAY_OF_MONTH, 1); // System.out.println(format2.format(c2.getTime())); // System.out.println(format2.format(c.getTime())); // this returns java.util.Date LocalDate startOrEndDayOfQuarter = getStartOrEndDayOfQuarter(true); System.out.println(startOrEndDayOfQuarter); }*/ // } } /** * 获取指定时间段的季度 * 输出格式:[2021-Q1, 2021-Q2, 2021-Q3, 2021-Q4, 2022-Q1, 2022-Q2, 2022-Q3, 2022-Q4] * @param starTime * @param endTime * @return */ public List<String> getQuarter(String starTime,String endTime){ // String time = "2021-01-01"; // String time2 = "2022-12-30"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date dateTime = null; Date dateTime2 = null; try { dateTime = simpleDateFormat.parse(starTime); dateTime2 = simpleDateFormat.parse(endTime); } catch (ParseException e) { e.printStackTrace(); } ArrayList<String> quarters = new ArrayList<String>(); Calendar cal = Calendar.getInstance(); cal.setTime(dateTime); Calendar cal1 = Calendar.getInstance(); cal1.setTime(dateTime2); while (cal1.getTime().after(cal.getTime()) || cal1.getTime().equals(cal.getTime())) { int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int quarter = month % 3 == 0 ? (month / 3) : (month / 3) + 1; quarters.add(year+"-Q" + quarter); cal.add(Calendar.MONTH, 3); } System.out.println(quarters); return quarters; } /** * 获取两个日期之间的所有月份 输出格式[2016-02, 2016-03, 2016-04, 2016-05, 2016-06, 2016-07, * 2016-08, 2016-09, 2016-10, 2016-11, 2016-12, 2017-01]" * @param data1 * @param data2 * @return */ public List<String> getYearAndMonth(String data1,String data2){ // String data1 = "2016-02";// 开始时间 // String data2 = "2019-12";// 结束时间 List<String> list = new ArrayList<String>(); try { Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse(data1); Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(data2); Calendar calendar = Calendar.getInstance(); calendar.setTime(startDate); // 获取开始年份和开始月份 int startYear = calendar.get(Calendar.YEAR); int startMonth = calendar.get(Calendar.MONTH); // 获取结束年份和结束月份 calendar.setTime(endDate); int endYear = calendar.get(Calendar.YEAR); int endMonth = calendar.get(Calendar.MONTH); // for (int i = startYear; i <= endYear; i++) { String date = ""; if (startYear == endYear) { for (int j = startMonth; j <= endMonth; j++) { if (j < 9) { date = i + "-0" + (j + 1); } else { date = i + "-" + (j + 1); } list.add(date); } } else { if (i == startYear) { for (int j = startMonth; j < 12; j++) { if (j < 9) { date = i + "-0" + (j + 1); } else { date = i + "-" + (j + 1); } list.add(date); } } else if (i == endYear) { for (int j = 0; j <= endMonth; j++) { if (j < 9) { date = i + "-0" + (j + 1); } else { date = i + "-" + (j + 1); } list.add(date); } } else { for (int j = 0; j < 12; j++) { if (j < 9) { date = i + "-0" + (j + 1); } else { date = i + "-" + (j + 1); } list.add(date); } } } } // 所有的月份已经准备好 //System.out.println(list); for(int i = 0;i < list.size();i++){ System.out.println(list.get(i)); } } catch (Exception e) { e.printStackTrace(); } return list; } /** * 获取当前日期所在季度的开始日期和结束日期 * 季度一年四季, 第一季度:1月-3月, 第二季度:4月-6月, 第三季度:7月-9月, 第四季度:10月-12月 * @param isFirst true表示查询本季度开始日期 false表示查询本季度结束日期 * @return */ public static LocalDate getStartOrEndDayOfQuarter(Boolean isFirst){ LocalDate today=LocalDate.now(); LocalDate resDate = LocalDate.now(); if (today == null) { today = resDate; } Month month = today.getMonth(); Month firstMonthOfQuarter = month.firstMonthOfQuarter(); Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2); if (isFirst) { resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1); } else { resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear())); } return resDate; } public static void getDateTime() throws ParseException{ Date date = new Date(); Calendar now = Calendar.getInstance(); System.out.println("年: " + now.get(Calendar.YEAR)); System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + ""); System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH)); System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY)); System.out.println("分: " + now.get(Calendar.MINUTE)); System.out.println("秒: " + now.get(Calendar.SECOND)); System.out.println("当前时间毫秒数:" + now.getTimeInMillis()); System.out.println(now.getTime()); Date d = new Date(); System.out.println(d); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateNowStr = sdf.format(d); System.out.println("格式化后的日期:" + dateNowStr); String str = "2012-1-13 17:26:33"; //要跟上面sdf定义的格式一样 Date today = sdf.parse(str); System.out.println("字符串转成日期:" + today); } /** * 获取前x月的日期 * @param month * @return */ public Map<String,String> getNowAndBefore(int month){ Date now = new Date(); Date newDate = stepMonth(now, -month); Map<String,String> map =new HashMap<>(); // 现在时间 map.put("now",DATE_FORMAT.format(now)); // 之前时间 map.put("before",DATE_FORMAT.format(newDate)); return map; } /** * 获取前x月的日期 * @param month * @return (Data类型) */ public Map<String,Date> getNowAndBeforeData(int month){ Date now = new Date(); Date newDate = stepMonth(now, -month); Map<String,Date> map =new HashMap<>(); // 现在时间 map.put("now",now); // 之前时间 map.put("before",newDate); return map; } /** * 获取后x天的日期 * @param day * @return */ public Map<String,String> getNowAndChangeDay(int day){ Date now = new Date(); Date newDate = stepDay(now, day); Map<String,String> map =new HashMap<>(); // 现在时间 map.put("now",DATE_FORMAT.format(now)); // 之后时间 map.put("change",DATE_FORMAT.format(newDate)); return map; } /** * 在给定的日期加上或减去指定月份后的日期 * @param sourceDate 原始时间 * @param month 要调整的月份,向前为负数,向后为正数 * @return */ public Date stepMonth(Date sourceDate, int month) { Calendar c = Calendar.getInstance(); c.setTime(sourceDate); c.add(Calendar.MONTH, month); return c.getTime(); } /** * 在给定的日期加上或减去指定月份后的日期 * @param * @param day 要调整的天数,向前为负数,向后为正数 * @return */ public Date stepDay(Date sourceDate, int day) { Calendar c = Calendar.getInstance(); c.setTime(sourceDate); c.add(Calendar.DAY_OF_MONTH, day); return c.getTime(); } /** * 输出如下: * "[2016-02, 2016-03, 2016-04, 2016-05, 2016-06, 2016-07, * 2016-08, 2016-09, 2016-10, 2016-11, 2016-12, 2017-01]" */ /** * 获取当前系统时间最近12月的年月(含当月) * @return */ public String[] getLatest12Month(){ String[] latest12Months = new String[12]; Calendar cal = Calendar.getInstance(); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1); //要先+1,才能把本月的算进去 for(int i=0; i<12; i++){ cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1); //逐次往前推1个月 latest12Months[11-i] = cal.get(Calendar.YEAR)+ "-" +fillZero(cal.get(Calendar.MONTH)+1); } System.out.println(Arrays.toString(latest12Months)); return latest12Months; } public List<String> getLast12Month(){ List<String> list =new ArrayList<>(); String[] latest12Month = getLatest12Month(); for (int i = 0; i < latest12Month.length; i++) { list.add(latest12Month[i]); } for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } return list; } /** * 格式化月份 */ public static String fillZero(int i){ String month = ""; if(i<10){ month = "0" + i; }else{ month = String.valueOf(i); } return month; } /** * 计算两个日期之间相差的天数 (Data类型的日期) * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */ public int daysBetweenData(Date smdate,Date bdate) throws ParseException { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); smdate=sdf.parse(sdf.format(smdate)); bdate=sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } /** *计算两个日期之间相差的天数(String类型的日期) * @param smdate 较小的时间 * @param bdate 较大的时间 * @return 相差天数 * @throws ParseException */ public int daysBetweenString(String smdate,String bdate) { Integer day = 0; try { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInstance(); cal.setTime(sdf.parse(smdate)); long time1 = cal.getTimeInMillis(); cal.setTime(sdf.parse(bdate)); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); day = Integer.parseInt(String.valueOf(between_days)); }catch (ParseException e ){ e.printStackTrace(); } return day; } }