在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

DateUtils日期操作Java类

2016-12-20 13:16| 发布者: zhangjf| 查看: 589| 评论: 0

摘要: 用于日期的各种操作,简单实用。。。 代码 import java.sql.Timestamp;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.lang3.ti ...
用于日期的各种操作,简单实用。。。

[Java]代码
  1. import java.sql.Timestamp;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import org.apache.commons.lang3.time.DateFormatUtils;
  6. /**
  7. * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
  8. * @author
  9. * @version 2015-10-12
  10. */
  11. public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
  12. private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
  13. "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };
  14. /**
  15. * 得到当前日期字符串 格式(yyyy-MM-dd)
  16. */
  17. public static String getDate() {
  18. return getDate("yyyy-MM-dd");
  19. }
  20. /**
  21. * 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
  22. */
  23. public static String getDate(String pattern) {
  24. return DateFormatUtils.format(new Date(), pattern);
  25. }
  26. /**
  27. * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
  28. */
  29. public static String formatDate(Date date, Object... pattern) {
  30. String formatDate = null;
  31. if (pattern != null && pattern.length > 0) {
  32. formatDate = DateFormatUtils.format(date, pattern[0].toString());
  33. } else {
  34. formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
  35. }
  36. return formatDate;
  37. }
  38. /**
  39. * 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
  40. */
  41. public static String formatDateTime(Date date) {
  42. return formatDate(date, "yyyy-MM-dd HH:mm:ss");
  43. }
  44. /**
  45. * 得到当前时间字符串 格式(HH:mm:ss)
  46. */
  47. public static String getTime() {
  48. return formatDate(new Date(), "HH:mm:ss");
  49. }
  50. /**
  51. * 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
  52. */
  53. public static String getDateTime() {
  54. return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
  55. }
  56. /**
  57. * 得到当前年份字符串 格式(yyyy)
  58. */
  59. public static String getYear() {
  60. return formatDate(new Date(), "yyyy");
  61. }
  62. /**
  63. * 得到当前月份字符串 格式(MM)
  64. */
  65. public static String getMonth() {
  66. return formatDate(new Date(), "MM");
  67. }
  68. /**
  69. * 得到当天字符串 格式(dd)
  70. */
  71. public static String getDay() {
  72. return formatDate(new Date(), "dd");
  73. }
  74. /**
  75. * 得到当前星期字符串 格式(E)星期几
  76. */
  77. public static String getWeek() {
  78. return formatDate(new Date(), "E");
  79. }
  80. /**
  81. * 日期型字符串转化为日期 格式
  82. * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
  83. * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }
  84. */
  85. public static Date parseDate(Object str) {
  86. if (str == null){
  87. return null;
  88. }
  89. try {
  90. return parseDate(str.toString(), parsePatterns);
  91. } catch (ParseException e) {
  92. return null;
  93. }
  94. }
  95. /**
  96. * 获取过去的天数
  97. * @param date
  98. * @return
  99. */
  100. public static long pastDays(Date date) {
  101. long t = new Date().getTime()-date.getTime();
  102. return t/(24*60*60*1000);
  103. }
  104. public static Date getDateStart(Date date) {
  105. if(date==null) {
  106. return null;
  107. }
  108. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  109. try {
  110. date= sdf.parse(formatDate(date, "yyyy-MM-dd")+" 00:00:00");
  111. } catch (ParseException e) {
  112. e.printStackTrace();
  113. }
  114. return date;
  115. }
  116. public static Date getDateEnd(Date date) {
  117. if(date==null) {
  118. return null;
  119. }
  120. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  121. try {
  122. date= sdf.parse(formatDate(date, "yyyy-MM-dd") +" 23:59:59");
  123. } catch (ParseException e) {
  124. e.printStackTrace();
  125. }
  126. return date;
  127. }
  128. /**
  129. * 判断字符串是否是日期
  130. * @param timeString
  131. * @return
  132. */
  133. public static boolean isDate(String timeString){
  134. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
  135. format.setLenient(false);
  136. try{
  137. format.parse(timeString);
  138. }catch(Exception e){
  139. return false;
  140. }
  141. return true;
  142. }
  143. /**
  144. * 格式化时间
  145. * @param timestamp
  146. * @return
  147. */
  148. public static String dateFormat(Date timestamp){
  149. SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  150. return format.format(timestamp);
  151. }
  152. /**
  153. * 获取系统时间Timestamp
  154. * @return
  155. */
  156. public static Timestamp getSysTimestamp(){
  157. return new Timestamp(new Date().getTime());
  158. }
  159. /**
  160. * 获取系统时间Date
  161. * @return
  162. */
  163. public static Date getSysDate(){
  164. return new Date();
  165. }
  166. /**
  167. * 生成时间随机数
  168. * @return
  169. */
  170. public static String getDateRandom(){
  171. String s=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
  172. return s;
  173. }
  174. /**
  175. * @param args
  176. * @throws ParseException
  177. */
  178. public static void main(String[] args) throws ParseException {
  179. // System.out.println(formatDate(parseDate("2010/3/6")));
  180. // System.out.println(getDate("yyyy年MM月dd日 E"));
  181. // long time = new Date().getTime()-parseDate("2012-11-19").getTime();
  182. // System.out.println(time/(24*60*60*1000));
  183. }
  184. }
复制代码
commons-lang3-3.1.jar ~ 308KB commons-lang3-3.1-sources.jar ~ 384KB (12)

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 14:40

Copyright 2015-2025 djqfx

返回顶部