You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
9.2 KiB

6 months ago
2 years ago
1 year ago
  1. package com.xujie.sys.common.utils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.joda.time.DateTime;
  4. import org.joda.time.LocalDate;
  5. import org.joda.time.format.DateTimeFormat;
  6. import org.joda.time.format.DateTimeFormatter;
  7. import java.text.ParseException;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. /**
  14. * 日期处理
  15. *
  16. *
  17. */
  18. public class DateUtils {
  19. /** 时间格式(yyyy-MM-dd) */
  20. public final static String DATE_PATTERN = "yyyy-MM-dd";
  21. /** 时间格式(yyyy-MM-dd HH:mm:ss) */
  22. public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
  23. /**
  24. * 日期格式化 日期格式为yyyy-MM-dd
  25. * @param date 日期
  26. * @return 返回yyyy-MM-dd格式日期
  27. */
  28. public static String format(Date date) {
  29. return format(date, DATE_PATTERN);
  30. }
  31. /**
  32. * 日期格式化 日期格式为yyyy-MM-dd
  33. * @param date 日期
  34. * @param pattern 格式DateUtils.DATE_TIME_PATTERN
  35. * @return 返回yyyy-MM-dd格式日期
  36. */
  37. public static String format(Date date, String pattern) {
  38. if(date != null){
  39. SimpleDateFormat df = new SimpleDateFormat(pattern);
  40. return df.format(date);
  41. }
  42. return null;
  43. }
  44. /**
  45. * 字符串转换成日期
  46. * @param strDate 日期字符串
  47. * @param pattern 日期的格式DateUtils.DATE_TIME_PATTERN
  48. */
  49. public static Date stringToDate(String strDate, String pattern) {
  50. if (StringUtils.isBlank(strDate)){
  51. return null;
  52. }
  53. DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
  54. return fmt.parseLocalDateTime(strDate).toDate();
  55. }
  56. /**
  57. * 根据周数获取开始日期结束日期
  58. * @param week 周期 0本周-1上周-2上上周1下周2下下周
  59. * @return 返回date[0]开始日期date[1]结束日期
  60. */
  61. public static Date[] getWeekStartAndEnd(int week) {
  62. DateTime dateTime = new DateTime();
  63. LocalDate date = new LocalDate(dateTime.plusWeeks(week));
  64. date = date.dayOfWeek().withMinimumValue();
  65. Date beginDate = date.toDate();
  66. Date endDate = date.plusDays(6).toDate();
  67. return new Date[]{beginDate, endDate};
  68. }
  69. /**
  70. * 对日期的进行加/
  71. *
  72. * @param date 日期
  73. * @param seconds 秒数负数为减
  74. * @return /减几秒后的日期
  75. */
  76. public static Date addDateSeconds(Date date, int seconds) {
  77. DateTime dateTime = new DateTime(date);
  78. return dateTime.plusSeconds(seconds).toDate();
  79. }
  80. /**
  81. * 对日期的分钟进行加/
  82. *
  83. * @param date 日期
  84. * @param minutes 分钟数负数为减
  85. * @return /减几分钟后的日期
  86. */
  87. public static Date addDateMinutes(Date date, int minutes) {
  88. DateTime dateTime = new DateTime(date);
  89. return dateTime.plusMinutes(minutes).toDate();
  90. }
  91. /**
  92. * 对日期的小时进行加/
  93. *
  94. * @param date 日期
  95. * @param hours 小时数负数为减
  96. * @return /减几小时后的日期
  97. */
  98. public static Date addDateHours(Date date, int hours) {
  99. DateTime dateTime = new DateTime(date);
  100. return dateTime.plusHours(hours).toDate();
  101. }
  102. /**
  103. * 对日期的进行加/
  104. *
  105. * @param date 日期
  106. * @param days 天数负数为减
  107. * @return /减几天后的日期
  108. */
  109. public static Date addDateDays(Date date, int days) {
  110. DateTime dateTime = new DateTime(date);
  111. return dateTime.plusDays(days).toDate();
  112. }
  113. /**
  114. * 对日期的进行加/
  115. *
  116. * @param date 日期
  117. * @param weeks 周数负数为减
  118. * @return /减几周后的日期
  119. */
  120. public static Date addDateWeeks(Date date, int weeks) {
  121. DateTime dateTime = new DateTime(date);
  122. return dateTime.plusWeeks(weeks).toDate();
  123. }
  124. /**
  125. * 对日期的进行加/
  126. *
  127. * @param date 日期
  128. * @param months 月数负数为减
  129. * @return /减几月后的日期
  130. */
  131. public static Date addDateMonths(Date date, int months) {
  132. DateTime dateTime = new DateTime(date);
  133. return dateTime.plusMonths(months).toDate();
  134. }
  135. /**
  136. * 对日期的进行加/
  137. *
  138. * @param date 日期
  139. * @param years 年数负数为减
  140. * @return /减几年后的日期
  141. */
  142. public static Date addDateYears(Date date, int years) {
  143. DateTime dateTime = new DateTime(date);
  144. return dateTime.plusYears(years).toDate();
  145. }
  146. // /**
  147. // * @Author sxm
  148. // * @Description 两个日期相差年份
  149. // * @Date 2022/4/12 14:47
  150. // * @Param
  151. // * @return
  152. // **/
  153. // public static int differentDays(Date date1,Date date2)
  154. // {
  155. // Calendar cal1 = Calendar.getInstance();
  156. // cal1.setTime(date1);
  157. //
  158. // Calendar cal2 = Calendar.getInstance();
  159. // cal2.setTime(date2);
  160. // int day1= cal1.get(Calendar.DAY_OF_YEAR);
  161. // int day2 = cal2.get(Calendar.DAY_OF_YEAR);
  162. //
  163. // int year1 = cal1.get(Calendar.YEAR);
  164. // int year2 = cal2.get(Calendar.YEAR);
  165. // if(year1 != year2) //同一年
  166. // {
  167. // int timeDistance = 0 ;
  168. // for(int i = year1 ; i < year2 ; i ++)
  169. // {
  170. // if(i%4==0 && i%100!=0 || i%400==0) //闰年
  171. // {
  172. // timeDistance += 366;
  173. // }
  174. // else //不是闰年
  175. // {
  176. // timeDistance += 365;
  177. // }
  178. // }
  179. //
  180. // return timeDistance + (day2-day1) ;
  181. // }
  182. // else { // 不同年
  183. // return day2-day1;
  184. // }
  185. // }
  186. public static String getStringNow() {
  187. SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_PATTERN);
  188. return sdf.format(new Date());
  189. }
  190. public static Date getStringToDate(String time,String format) {
  191. SimpleDateFormat sdf = new SimpleDateFormat(format);
  192. try {
  193. return sdf.parse(time);
  194. } catch (ParseException e) {
  195. throw new RuntimeException(e);
  196. }
  197. }
  198. /**
  199. * date2比date1多的天数
  200. * @param date1
  201. * @param date2
  202. * @return
  203. */
  204. public static int differentDays (Date date1, Date date2) {
  205. Calendar cal1 = Calendar.getInstance();
  206. cal1.setTime(date1);
  207. Calendar cal2 = Calendar.getInstance();
  208. cal2.setTime(date2);
  209. int day1= cal1.get(Calendar.DAY_OF_YEAR);
  210. int day2 = cal2.get(Calendar.DAY_OF_YEAR);
  211. int year1 = cal1.get(Calendar.YEAR);
  212. int year2 = cal2.get(Calendar.YEAR);
  213. if(year1 != year2) { // 同一年
  214. int timeDistance = 0 ;
  215. for(int i = year1; i < year2; i ++) {
  216. if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { // 闰年
  217. timeDistance += 366;
  218. } else { // 不是闰年
  219. timeDistance += 365;
  220. }
  221. }
  222. return timeDistance + (day2-day1);
  223. } else { // 不同年
  224. return day2 - day1;
  225. }
  226. }
  227. /**
  228. * 比较两个日期的大小
  229. * 2 发生异常
  230. * 1 firstDate>secondDate
  231. * 0 firstDate==secondDate
  232. * -1 firstDate<secondDate
  233. * @param format
  234. * @param firstDate
  235. * @param secondDate
  236. * @return
  237. */
  238. public static int compareDate(String format,String firstDate, String secondDate){
  239. SimpleDateFormat sdf = new SimpleDateFormat(format);
  240. int result = 2;
  241. try {
  242. Date fDate = sdf.parse(firstDate);
  243. Date sDate = sdf.parse(secondDate);
  244. if(fDate.getTime()>sDate.getTime()){
  245. result = 1;
  246. }else if(fDate.getTime()==sDate.getTime()){
  247. result = 0;
  248. }else{
  249. result = -1;
  250. }
  251. } catch (ParseException ex) {
  252. Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);
  253. }
  254. return result;
  255. }
  256. public static String getStringDate(Date date) {
  257. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  258. return sdf.format(date);
  259. }
  260. public static Date getDateDate(Date currentDate) throws ParseException {
  261. // 使用 SimpleDateFormat 格式化日期
  262. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  263. String formattedDate = sdf.format(currentDate);
  264. // 将格式化的字符串再转换回 Date 对象
  265. Date dateWithoutTime = sdf.parse(formattedDate);
  266. return dateWithoutTime;
  267. }
  268. public static void main(String[] args) {
  269. try {
  270. try {
  271. getDateByParten("777","yyyy-MM-dd");
  272. }catch (ParseException e){
  273. throw new RuntimeException("格式有误!");
  274. }
  275. }catch (Exception e){
  276. System.out.println(e.getMessage()+"66666");
  277. }
  278. }
  279. public static Date getDateByParten(String date, String parten)
  280. throws ParseException {
  281. SimpleDateFormat sdf = new SimpleDateFormat(parten);
  282. return sdf.parse(date);
  283. }
  284. }