一个Java时间工具类_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 一个Java时间工具类

一个Java时间工具类

 2012/4/15 1:22:01  long_yu2  程序员俱乐部  我要评论(0)
  • 摘要:主要包含了对时间操作的一些工具方法,仅供参考吧!源码如下:Java代码packagecom..util;importjava.sql.Timestamp;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;publicclassDataUtil{/***功能:将日期对象按照某种格式进行转换,返回转换后的字符串**@paramdate日期对象*@parampattern转换格式例:yyyy
  • 标签:工具 Java 一个

主要包含了对时间操作的一些工具方法,仅供参考吧!

源码如下:

Java代码??收藏代码
  1. package?com..util;??
  2. ??
  3. import?java.sql.Timestamp;??
  4. import?java.text.SimpleDateFormat;??
  5. import?java.util.Calendar;??
  6. import?java.util.Date;??
  7. ??
  8. public?class?DataUtil?{??
  9. ??
  10. ????/**?
  11. ?????*?功能:?将日期对象按照某种格式进行转换,返回转换后的字符串?
  12. ?????*??
  13. ?????*?@param?date?日期对象?
  14. ?????*?@param?pattern?转换格式?例:yyyy-MM-dd?
  15. ?????*/??
  16. ????public?static?String?DateToString(Date?date,?String?pattern)?{??
  17. ????????String?strDateTime?=?null;??
  18. ????????SimpleDateFormat?formater?=?new?SimpleDateFormat(pattern);??
  19. ????????strDateTime?=?date?==?null???null?:?formater.format(date);??
  20. ????????return?strDateTime;??
  21. ????}??
  22. ??
  23. ????/**?
  24. ?????*?功能:?将传入的日期对象按照yyyy-MM-dd格式转换成字符串返回?
  25. ?????*??
  26. ?????*?@param?date?日期对象?
  27. ?????*?@return?String?
  28. ?????*/??
  29. ????public?static?String?DateToString(Date?date)?{??
  30. ????????String?_pattern?=?"yyyy-MM-dd";??
  31. ????????return?date?==?null???null?:?DateToString(date,?_pattern);??
  32. ????}??
  33. ??
  34. ????/**?
  35. ?????*?功能:?将传入的日期对象按照yyyy-MM-dd?HH:mm:ss格式转换成字符串返回?
  36. ?????*??
  37. ?????*?@param?date?日期对象?
  38. ?????*?@return?String?
  39. ?????*/??
  40. ????public?static?String?DateTimeToString(Date?date)?{??
  41. ????????String?_pattern?=?"yyyy-MM-dd?HH:mm:ss";??
  42. ????????return?date?==?null???null?:?DateToString(date,?_pattern);??
  43. ????}??
  44. ??
  45. ????/**?
  46. ?????*?功能:?将插入的字符串按格式转换成对应的日期对象?
  47. ?????*??
  48. ?????*?@param?str?字符串?
  49. ?????*?@param?pattern?格式?
  50. ?????*?@return?Date?
  51. ?????*/??
  52. ????public?static?Date?StringToDate(String?str,?String?pattern)?{??
  53. ????????Date?dateTime?=?null;??
  54. ????????try?{??
  55. ????????????if?(str?!=?null?&&?!str.equals(""))?{??
  56. ????????????????SimpleDateFormat?formater?=?new?SimpleDateFormat(pattern);??
  57. ????????????????dateTime?=?formater.parse(str);??
  58. ????????????}??
  59. ????????}?catch?(Exception?ex)?{??
  60. ????????}??
  61. ????????return?dateTime;??
  62. ????}??
  63. ??
  64. ????/**?
  65. ?????*?功能:?将传入的字符串按yyyy-MM-dd格式转换成对应的日期对象?
  66. ?????*??
  67. ?????*?@param?str?需要转换的字符串?
  68. ?????*?@return?Date?返回值?
  69. ?????*/??
  70. ????public?static?Date?StringToDate(String?str)?{??
  71. ????????String?_pattern?=?"yyyy-MM-dd";??
  72. ????????return?StringToDate(str,?_pattern);??
  73. ????}??
  74. ??
  75. ????/**?
  76. ?????*?功能:?将传入的字符串按yyyy-MM-dd?HH:mm:ss格式转换成对应的日期对象?
  77. ?????*??
  78. ?????*?@param?str?需要转换的字符串?
  79. ?????*?@return?Date?
  80. ?????*/??
  81. ????public?static?Date?StringToDateTime(String?str)?{??
  82. ????????String?_pattern?=?"yyyy-MM-dd?HH:mm:ss";??
  83. ????????return?StringToDate(str,?_pattern);??
  84. ????}??
  85. ??
  86. ????/**?
  87. ?????*?功能:?将传入的字符串转换成对应的Timestamp对象?
  88. ?????*??
  89. ?????*?@param?str?待转换的字符串?
  90. ?????*?@return?Timestamp?转换之后的对象?
  91. ?????*?@throws?Exception?
  92. ?????*?????????????Timestamp?
  93. ?????*/??
  94. ????public?static?Timestamp?StringToDateHMS(String?str)?throws?Exception?{??
  95. ????????Timestamp?time?=?null;??
  96. ????????time?=?Timestamp.valueOf(str);??
  97. ????????return?time;??
  98. ????}??
  99. ??
  100. ????/**?
  101. ?????*?功能:?根据传入的年月日返回相应的日期对象?
  102. ?????*??
  103. ?????*?@param?year?年份?
  104. ?????*?@param?month?月份?
  105. ?????*?@param?day?天?
  106. ?????*?@return?Date?日期对象?
  107. ?????*/??
  108. ????public?static?Date?YmdToDate(int?year,?int?month,?int?day)?{??
  109. ????????Calendar?calendar?=?Calendar.getInstance();??
  110. ????????calendar.set(year,?month,?day);??
  111. ????????return?calendar.getTime();??
  112. ????}??
  113. ??
  114. ????/**?
  115. ?????*?功能:?将日期对象按照MM/dd?HH:mm:ss的格式进行转换,返回转换后的字符串?
  116. ?????*??
  117. ?????*?@param?date?日期对象?
  118. ?????*?@return?String?返回值?
  119. ?????*/??
  120. ????public?static?String?communityDateToString(Date?date)?{??
  121. ????????SimpleDateFormat?formater?=?new?SimpleDateFormat("MM/dd?HH:mm:ss");??
  122. ????????String?strDateTime?=?date?==?null???null?:?formater.format(date);??
  123. ????????return?strDateTime;??
  124. ????}??
  125. ??
  126. ????public?static?Date?getMaxDateOfDay(Date?date)?{??
  127. ????????if?(date?==?null)?{??
  128. ????????????return?null;??
  129. ????????}?else?{??
  130. ????????????Calendar?calendar?=?Calendar.getInstance();??
  131. ????????????calendar.setTime(date);??
  132. ????????????calendar.set(11,?calendar.getActualMaximum(11));??
  133. ????????????calendar.set(12,?calendar.getActualMaximum(12));??
  134. ????????????calendar.set(13,?calendar.getActualMaximum(13));??
  135. ????????????calendar.set(14,?calendar.getActualMaximum(14));??
  136. ????????????return?calendar.getTime();??
  137. ????????}??
  138. ????}??
  139. ??
  140. ????public?static?Date?getMinDateOfDay(Date?date)?{??
  141. ????????if?(date?==?null)?{??
  142. ????????????return?null;??
  143. ????????}?else?{??
  144. ????????????Calendar?calendar?=?Calendar.getInstance();??
  145. ????????????calendar.setTime(date);??
  146. ????????????calendar.set(11,?calendar.getActualMinimum(11));??
  147. ????????????calendar.set(12,?calendar.getActualMinimum(12));??
  148. ????????????calendar.set(13,?calendar.getActualMinimum(13));??
  149. ????????????calendar.set(14,?calendar.getActualMinimum(14));??
  150. ????????????return?calendar.getTime();??
  151. ????????}??
  152. ????}??
  153. ??
  154. ????/**?
  155. ?????*?功能:返回传入日期对象(date)之后afterDays天数的日期对象?
  156. ?????*??
  157. ?????*?@param?date?日期对象?
  158. ?????*?@param?afterDays?往后天数?
  159. ?????*?@return?java.util.Date?返回值?
  160. ?????*/??
  161. ????public?static?Date?getAfterDay(Date?date,?int?afterDays)?{??
  162. ????????Calendar?cal?=?Calendar.getInstance();??
  163. ????????cal.setTime(date);??
  164. ????????cal.add(Calendar.DATE,?1);??
  165. ????????return?cal.getTime();??
  166. ????}??
  167. ??
  168. ????//?day??
  169. ????/**?
  170. ?????*?功能:?返回date1与date2相差的天数?
  171. ?????*??
  172. ?????*?@param?date1?
  173. ?????*?@param?date2?
  174. ?????*?@return?int?
  175. ?????*/??
  176. ????public?static?int?DateDiff(Date?date1,?Date?date2)?{??
  177. ????????int?i?=?(int)?((date1.getTime()?-?date2.getTime())?/?3600?/?24?/?1000);??
  178. ????????return?i;??
  179. ????}??
  180. ??
  181. ????//?min??
  182. ????/**?
  183. ?????*?功能:?返回date1与date2相差的分钟数?
  184. ?????*??
  185. ?????*?@param?date1?
  186. ?????*?@param?date2?
  187. ?????*?@return?int?
  188. ?????*/??
  189. ????public?static?int?MinDiff(Date?date1,?Date?date2)?{??
  190. ????????int?i?=?(int)?((date1.getTime()?-?date2.getTime())?/?1000?/?60);??
  191. ????????return?i;??
  192. ????}??
  193. ??
  194. ????//?second??
  195. ????/**?
  196. ?????*?功能:?返回date1与date2相差的秒数?
  197. ?????*??
  198. ?????*?@param?date1?
  199. ?????*?@param?date2?
  200. ?????*?@return?int?
  201. ?????*/??
  202. ????public?static?int?TimeDiff(Date?date1,?Date?date2)?{??
  203. ????????int?i?=?(int)?((date1.getTime()?-?date2.getTime()));??
  204. ????????return?i;??
  205. ????}??
  206. ??
  207. }??

?

  • DataUtil.rar?(1.2 KB)
  • 下载次数: 12
发表评论
用户名: 匿名