DateUtils.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package com.template.common.utils;
  2. import org.springframework.stereotype.Component;
  3. import java.sql.Timestamp;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Calendar;
  7. import java.util.Date;
  8. import java.util.GregorianCalendar;
  9. /**
  10. * <p>Title: DateUtils</p>
  11. * <p>Description:日期工具类 </p>
  12. *
  13. * @author fengyong
  14. * @date 2018年9月7日
  15. */
  16. @Component // 加此注解是把此类实例化spring容器中
  17. public class DateUtils {
  18. /**
  19. * 默认日期格式
  20. */
  21. public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss";
  22. /**
  23. * 如2018 0901 232211(年月日时分秒)
  24. *
  25. * @return
  26. */
  27. public String yyyyMMddHHmmss() {
  28. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  29. return sdf.format(Calendar.getInstance().getTime());
  30. }
  31. /**
  32. * 如20180901
  33. *
  34. * @return
  35. */
  36. public static String getYYYYMMdd() {
  37. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
  38. return sdf.format(Calendar.getInstance().getTime());
  39. }
  40. /**
  41. * 如180901
  42. * @return
  43. */
  44. public String getYYMMdd() {
  45. SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
  46. return sdf.format(Calendar.getInstance().getTime());
  47. }
  48. /**
  49. * 如201809
  50. * @return
  51. */
  52. public String getYYYYMM() {
  53. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
  54. return sdf.format(Calendar.getInstance().getTime());
  55. }
  56. /**
  57. * 如 2018/02/11
  58. * @return
  59. */
  60. public String getQueryEndDate() {
  61. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  62. return sdf.format(Calendar.getInstance().getTime());
  63. }
  64. /**
  65. * 如 2018/02/11
  66. * @return
  67. */
  68. public String getQueryStartDate() {
  69. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  70. Calendar cal = Calendar.getInstance();
  71. cal.add(Calendar.DAY_OF_YEAR, -30);
  72. return sdf.format(cal.getTime());
  73. }
  74. /**
  75. * 如 2018/02/11 12:30:00
  76. * @return
  77. */
  78. public static String getQueryEndTime() {
  79. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  80. return sdf.format(Calendar.getInstance().getTime());
  81. }
  82. /**
  83. * 如 2018/02/11
  84. * @return
  85. */
  86. public String getQueryStartTime() {
  87. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  88. Calendar cal = Calendar.getInstance();
  89. cal.add(Calendar.DAY_OF_YEAR, -30);
  90. return sdf.format(cal.getTime());
  91. }
  92. public String getQueryTwoAgoDate() {
  93. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  94. Calendar cal = Calendar.getInstance();
  95. cal.add(Calendar.DAY_OF_YEAR, -2);
  96. return sdf.format(cal.getTime());
  97. }
  98. /**
  99. * 字符串转换成日期
  100. *
  101. * @param str 字符串
  102. * @param format 日期格式
  103. * @return 日期
  104. */
  105. public static Date str2Date(String str, String format) {
  106. if (null == str || "".equals(str)) {
  107. return null;
  108. }
  109. // 如果没有指定字符串转换的格式,则用默认格式进行转换
  110. if (null == format || "".equals(format)) {
  111. format = DEFAULT_FORMAT;
  112. }
  113. SimpleDateFormat sdf = new SimpleDateFormat(format);
  114. Date date = null;
  115. try {
  116. date = sdf.parse(str);
  117. return date;
  118. } catch (ParseException e) {
  119. }
  120. return null;
  121. }
  122. /**
  123. * @param date 日期
  124. * @param format 日期格式
  125. * @return 字符串
  126. */
  127. public static String date2Str(Date date, String format) {
  128. if (null == date) {
  129. return null;
  130. }
  131. SimpleDateFormat sdf = new SimpleDateFormat(format);
  132. return sdf.format(date);
  133. }
  134. /**
  135. * 时间戳转换为字符串
  136. * @param time
  137. * @return
  138. */
  139. public static String timestamp2Str(Timestamp time) {
  140. Date date = new Date(time.getTime());
  141. return date2Str(date, DEFAULT_FORMAT);
  142. }
  143. /**
  144. * 字符串转换为时间
  145. * @param str
  146. * @return
  147. */
  148. public static Timestamp str2Timestamp(String str) {
  149. Date date = str2Date(str, DEFAULT_FORMAT);
  150. return new Timestamp(date.getTime());
  151. }
  152. /**
  153. * 字符串转换为时间
  154. * @param str
  155. * @return
  156. */
  157. public static Timestamp str2Timestamp(String str, String formatStr) {
  158. if (null == str) {
  159. return null;
  160. }
  161. Date date = str2Date(str, formatStr);
  162. return new Timestamp(date.getTime());
  163. }
  164. /**
  165. * 获取某年第一天日期
  166. * @param year 年份
  167. * @return Date
  168. */
  169. public static Date getYearFirst(int year) {
  170. Calendar calendar = Calendar.getInstance();
  171. calendar.clear();
  172. calendar.set(Calendar.YEAR, year);
  173. Date currYearFirst = calendar.getTime();
  174. return currYearFirst;
  175. }
  176. /**
  177. * 获取某年最后一天日期
  178. * @param year 年份
  179. * @return Date
  180. */
  181. public static Date getYearLast(int year) {
  182. Calendar calendar = Calendar.getInstance();
  183. calendar.clear();
  184. calendar.set(Calendar.YEAR, year);
  185. calendar.roll(Calendar.DAY_OF_YEAR, -1);
  186. Date currYearLast = calendar.getTime();
  187. return currYearLast;
  188. }
  189. @SuppressWarnings("static-access")
  190. public static Date getnextLast(String datetime, int year) {
  191. Calendar calendar = new GregorianCalendar();
  192. Date date = null;
  193. if (datetime.length() > 7) {
  194. date = str2Date(datetime, "yyyy-MM-dd");
  195. } else {
  196. date = str2Date(datetime, "yyyy-MM");
  197. }
  198. calendar.setTime(date);
  199. calendar.add(calendar.YEAR, year);// 把日期往后增加一年.整数往后推,负数往前移动
  200. date = calendar.getTime();
  201. return date;
  202. }
  203. public static String getrightDate(String datetime, int year) {
  204. String date = "";
  205. String years = datetime.substring(0, 4);
  206. String dates = datetime.substring(4, datetime.length());
  207. Integer s = new Integer(years) + year;
  208. date = s + dates;
  209. return date;
  210. }
  211. public static void main(String[] args) {
  212. logger.info(getrightDate("2018-09", 4));
  213. logger.info(date2Str(getnextLast("2018-09", 4), "yyyy-MM"));
  214. }
  215. }