|
|
@ -1,8 +1,8 @@ |
|
|
|
/** |
|
|
|
* Copyright (c) 2018 人人开源 All rights reserved. |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* https://www.renren.io
|
|
|
|
* |
|
|
|
* <p> |
|
|
|
* 版权所有,侵权必究! |
|
|
|
*/ |
|
|
|
|
|
|
@ -16,8 +16,7 @@ import org.joda.time.format.DateTimeFormatter; |
|
|
|
|
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.util.Calendar; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
/** |
|
|
|
* 日期处理工具类 |
|
|
@ -27,12 +26,18 @@ import java.util.Date; |
|
|
|
*/ |
|
|
|
public class DateUtils { |
|
|
|
|
|
|
|
/** 时间格式(yyyy-MM-dd) */ |
|
|
|
public final static String DATE_PATTERN = "yyyy-MM-dd"; |
|
|
|
/** 时间格式(yyyy-MM-dd HH:mm:ss) */ |
|
|
|
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; |
|
|
|
/** 季度月份列表 */ |
|
|
|
public static final List<Integer> Q1Months = Arrays.asList(1, 2, 3); |
|
|
|
public static final List<Integer> Q2Months = Arrays.asList(4, 5, 6); |
|
|
|
public static final List<Integer> Q3Months = Arrays.asList(7, 8, 9); |
|
|
|
public static final List<Integer> Q4Months = Arrays.asList(10, 11, 12); |
|
|
|
|
|
|
|
/** 时间格式(yyyy-MM-dd) */ |
|
|
|
public final static String DATE_PATTERN = "yyyy-MM-dd"; |
|
|
|
/** 时间格式(yyyy-MM-dd HH:mm:ss) */ |
|
|
|
public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; |
|
|
|
/** 时间格式(yyyyMMddHHmmss) */ |
|
|
|
public final static String DATE_TIME_NO_SPLIT = "yyyyMMddHHmmss"; |
|
|
|
public final static String DATE_TIME_NO_SPLIT = "yyyyMMddHHmmss"; |
|
|
|
/** 时间格式(yyyy-MM-dd HH:mm) */ |
|
|
|
public final static String DATE_TIME_PATTERN_END_WITH_MINUTE = "yyyy-MM-dd HH:mm"; |
|
|
|
|
|
|
@ -48,9 +53,9 @@ public class DateUtils { |
|
|
|
/** |
|
|
|
* 日期格式化 日期格式为:yyyy-MM-dd |
|
|
|
* @param date 日期 |
|
|
|
* @return 返回yyyy-MM-dd格式日期 |
|
|
|
* @return 返回yyyy-MM-dd格式日期 |
|
|
|
*/ |
|
|
|
public static String format(Date date) { |
|
|
|
public static String format(Date date) { |
|
|
|
return format(date, DATE_PATTERN); |
|
|
|
} |
|
|
|
|
|
|
@ -58,10 +63,10 @@ public class DateUtils { |
|
|
|
* 日期格式化 日期格式为:yyyy-MM-dd |
|
|
|
* @param date 日期 |
|
|
|
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN |
|
|
|
* @return 返回yyyy-MM-dd格式日期 |
|
|
|
* @return 返回yyyy-MM-dd格式日期 |
|
|
|
*/ |
|
|
|
public static String format(Date date, String pattern) { |
|
|
|
if(date != null){ |
|
|
|
if (date != null) { |
|
|
|
SimpleDateFormat df = new SimpleDateFormat(pattern); |
|
|
|
return df.format(date); |
|
|
|
} |
|
|
@ -72,7 +77,7 @@ public class DateUtils { |
|
|
|
* 日期解析 |
|
|
|
* @param date 日期 |
|
|
|
* @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN |
|
|
|
* @return 返回Date |
|
|
|
* @return 返回Date |
|
|
|
*/ |
|
|
|
public static Date parse(String date, String pattern) { |
|
|
|
try { |
|
|
@ -89,7 +94,7 @@ public class DateUtils { |
|
|
|
* @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN |
|
|
|
*/ |
|
|
|
public static Date stringToDate(String strDate, String pattern) { |
|
|
|
if (StringUtils.isBlank(strDate)){ |
|
|
|
if (StringUtils.isBlank(strDate)) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
@ -100,7 +105,7 @@ public class DateUtils { |
|
|
|
/** |
|
|
|
* 根据周数,获取开始日期、结束日期 |
|
|
|
* @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周 |
|
|
|
* @return 返回date[0]开始日期、date[1]结束日期 |
|
|
|
* @return 返回date[0]开始日期、date[1]结束日期 |
|
|
|
*/ |
|
|
|
public static Date[] getWeekStartAndEnd(int week) { |
|
|
|
DateTime dateTime = new DateTime(); |
|
|
@ -205,8 +210,8 @@ public class DateUtils { |
|
|
|
Calendar calendar = Calendar.getInstance(); |
|
|
|
calendar.setTime(date); |
|
|
|
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); |
|
|
|
String[] chineseWeekDayName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; |
|
|
|
String[] englishWeekDayName = { "Sunday", "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" }; |
|
|
|
String[] chineseWeekDayName = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; |
|
|
|
String[] englishWeekDayName = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; |
|
|
|
|
|
|
|
String weekDayName; |
|
|
|
switch (type) { |
|
|
@ -265,22 +270,65 @@ public class DateUtils { |
|
|
|
public static int getQuarterIndex(Date date) { |
|
|
|
LocalDate localDate = new LocalDate(date); |
|
|
|
int monthOfYear = localDate.getMonthOfYear(); |
|
|
|
if (monthOfYear == 1 || monthOfYear == 2 || monthOfYear == 3) { |
|
|
|
if (Q1Months.contains(monthOfYear)) { |
|
|
|
return 1; |
|
|
|
} |
|
|
|
if (monthOfYear == 4 || monthOfYear == 5 || monthOfYear == 6) { |
|
|
|
if (Q2Months.contains(monthOfYear)) { |
|
|
|
return 2; |
|
|
|
} |
|
|
|
if (monthOfYear == 7 || monthOfYear == 8 || monthOfYear == 9) { |
|
|
|
if (Q3Months.contains(monthOfYear)) { |
|
|
|
return 3; |
|
|
|
} |
|
|
|
return 4; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 根据季度查询季度的月份列表 |
|
|
|
* @param quarterIndex |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static List<Integer> getMonthsByQuarterIndex(Integer quarterIndex) { |
|
|
|
switch (quarterIndex) { |
|
|
|
case 1: |
|
|
|
return Q1Months; |
|
|
|
case 2: |
|
|
|
return Q2Months; |
|
|
|
case 3: |
|
|
|
return Q3Months; |
|
|
|
default: |
|
|
|
return Q4Months; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取季度的开始date |
|
|
|
* @param year |
|
|
|
* @param quarterIndex |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Date getQuarterStartDate(String year, Integer quarterIndex) { |
|
|
|
List<Integer> months = getMonthsByQuarterIndex(quarterIndex); |
|
|
|
String yyyyMM = year.concat(String.format("%02d", months.get(0))); |
|
|
|
return DateUtils.parse(yyyyMM, DateUtils.DATE_PATTERN_YYYYMM); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取季度的结束date |
|
|
|
* @param year |
|
|
|
* @param quarterIndex |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static Date getQuarterEndDate(String year, Integer quarterIndex) { |
|
|
|
List<Integer> months = getMonthsByQuarterIndex(quarterIndex); |
|
|
|
String yyyyMM = year.concat(String.format("%02d", months.get(2))); |
|
|
|
Date nextMonth = DateUtils.addDateMonths(DateUtils.parse(yyyyMM, DateUtils.DATE_PATTERN_YYYYMM), 1); |
|
|
|
// 用下一个季度开头-1天就是上一个月季度最后一天
|
|
|
|
return DateUtils.addDateDays(nextMonth, -1); |
|
|
|
} |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
//int weekOfYear = getWeekOfYear(new Date());
|
|
|
|
|
|
|
|
int quarterIndex = DateUtils.getQuarterIndex(DateUtils.parse("20201001", DateUtils.DATE_PATTERN_YYYYMMDD)); |
|
|
|
String format = String.format("%02d", 9); |
|
|
|
|
|
|
|
System.out.println(666); |
|
|
|
} |
|
|
|