java里时间的转换

java里时间的转换

备注:从页面获取时间类型的值,String转换为Date,再从java.util.Date转为java.sql.Date,或者java.sql.Timestamp转为数据库中的datetime类型。发现代码变得比较凌乱,考虑不想使用第三方jar包,所以就自己写了一个简洁的转换工具类。



  1. import java.sql.Timestamp;
  2. import java.text.DateFormat;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Locale;


  7. /**
  8.  * 日期时间转换 说明: 
  9.  * 1、java.sql.Timestamp 对应 masql下的 datetime
  10.  * 2、java.sql.Date 是  java.util.Date 的 子类
  11.  * 3、格式可以自定义  yyyy-MM-dd HH:mm:ss.s
  12.  * @author john
  13.  * 
  14.  */
  15. public final class DateUtils {


  16. // 获取java.util.Date
  17. // 只能参考使用,项目中禁止调用
  18. public final static Date getDate() {


  19. Date date = new Date();
  20. return date;


  21. }


  22. // 文本格式(String) ==》 Timestamp
  23. // 2010-05-31 10:45:23.1
  24. public final static Timestamp stringToTimestamp(String dateString)
  25. throws java.text.ParseException {
  26. DateFormat dateFormat;
  27. dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINESE);// 设定格式
  28. dateFormat.setLenient(false);// 严格控制输入 比如2010-02-31,根本没有这一天 ,也会认为时间格式不对。
  29. Date timeDate = dateFormat.parse(dateString);// util类型
  30. Timestamp dateTime = new Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型
  31. return dateTime;
  32. }
  33. // 获取系统 日期+时间 的毫秒数
  34. // 获取从1970年1月1日开始经历的毫秒数
  35. public final static long getDateLong() {


  36. Date date = new Date();
  37. return date.getTime();


  38. }
  39. // java.sql.Date ==》 java.util.Date
  40. // 子类 转 父类  不容易
  41. // 2013-12-26
  42. public final static java.util.Date sqlDateToUtilDate(java.sql.Date date) {


  43. String sqlDateToUtilDateString = date+"";
  44. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
  45. "yyyy-MM-dd");
  46. java.util.Date utilDate = null;
  47. // 文本格式String 转为 Date
  48. try {
  49. utilDate = simpleDateFormat.parse(sqlDateToUtilDateString);
  50. } catch (ParseException e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54. return utilDate;


  55. }
  56. // java.util.Date ==》 java.sql.Date
  57. // 父类 转 子类    容易
  58. // 2013-12-26
  59. public final static java.sql.Date utilDateToSqlDate(java.util.Date date) {


  60. return new java.sql.Date(date.getTime());


  61. }


  62. //==========================================================================
  63. // 自定义日期格式 + java.util.Date==》文本格式(String)
  64. // 2013-12-26
  65. public final static String dateToSimpleDateFormat(Date date) {


  66. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
  67. "yyyy-MM-dd");
  68. // 日期 转为 文本格式String
  69. return simpleDateFormat.format(date);
  70. }


  71. // 四个内建的日期格式 + java.util.Date==》文本格式(String)
  72. // 短的
  73. // 13-12-26 下午5:24
  74. public final static String dateToShortDateFormat(Date date) {


  75. DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
  76. DateFormat.SHORT, DateFormat.SHORT);
  77. // 日期 转为 文本格式String
  78. return shortDateFormat.format(date);
  79. }


  80. // 四个内建的日期格式 + java.util.Date==》文本格式(String)
  81. // 中的
  82. // 2013-12-26 17:25:15
  83. public final static String dateToMediumDateFormat(Date date) {


  84. DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
  85. DateFormat.MEDIUM, DateFormat.MEDIUM);
  86. // 日期 转为 文本格式String
  87. return mediumDateFormat.format(date);
  88. }


  89. // 四个内建的日期格式 + java.util.Date==》文本格式(String)
  90. // 长的
  91. // 2013年12月26日 下午05时26分44秒
  92. public final static String dateTolongDateFormat(Date date) {


  93. DateFormat longDateFormat = DateFormat.getDateTimeInstance(
  94. DateFormat.LONG, DateFormat.LONG);
  95. // 日期 转为 文本格式String
  96. return longDateFormat.format(date);
  97. }


  98. // 四个内建的日期格式 + java.util.Date==》文本格式(String)
  99. // 完整的
  100. // 2013年12月26日 星期四 下午05时27分03秒 CST
  101. public final static String dateToFullDateFormat(Date date) {


  102. DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
  103. DateFormat.FULL, DateFormat.FULL);
  104. // 日期 转为 文本格式String
  105. return fullDateFormat.format(date);
  106. }
  107. //==========================================================================
  108. // 自定义日期格式 + 文本格式(String)==》java.util.Date
  109. // 2013-12-26
  110. public final static Date simpleDateFormatStringToDate(String simpleDateFormatString) {


  111. SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
  112. "yyyy-MM-dd");
  113. Date date = null;
  114. // 文本格式String 转为 Date
  115. try {
  116. date = simpleDateFormat.parse(simpleDateFormatString);
  117. } catch (ParseException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121. return date;
  122. }
  123. // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
  124. // 短的
  125. // 13-12-26 下午5:24
  126. public final static Date shortDateFormatStringToDate(String shortDateFormatString) {


  127. DateFormat shortDateFormat = DateFormat.getDateTimeInstance(
  128. DateFormat.SHORT, DateFormat.SHORT);
  129. Date date = null;
  130. // 文本格式String 转为 Date
  131. try {
  132. date = shortDateFormat.parse(shortDateFormatString);
  133. } catch (ParseException e) {
  134. // TODO Auto-generated catch block
  135. e.printStackTrace();
  136. }
  137. return date;
  138. }
  139. // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
  140. // 中的
  141. // 2013-12-26 17:25:15
  142. public final static Date mediumDateFormatStringToDate(String mediumDateFormatString) {


  143. DateFormat mediumDateFormat = DateFormat.getDateTimeInstance(
  144. DateFormat.MEDIUM, DateFormat.MEDIUM);
  145. Date date = null;
  146. // 文本格式String 转为 Date
  147. try {
  148. date = mediumDateFormat.parse(mediumDateFormatString);
  149. } catch (ParseException e) {
  150. // TODO Auto-generated catch block
  151. e.printStackTrace();
  152. }
  153. return date;
  154. }
  155. // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
  156. // 长的
  157. // 2013年12月26日 下午05时26分44秒
  158. public final static Date longDateFormatStringToDate(String longDateFormatString) {


  159. DateFormat longDateFormat = DateFormat.getDateTimeInstance(
  160. DateFormat.LONG, DateFormat.LONG);
  161. Date date = null;
  162. // 文本格式String 转为 Date
  163. try {
  164. date = longDateFormat.parse(longDateFormatString);
  165. } catch (ParseException e) {
  166. // TODO Auto-generated catch block
  167. e.printStackTrace();
  168. }
  169. return date;
  170. }
  171. // 四个内建的日期格式 + 文本格式(String)==》java.util.Date
  172. // 完整的
  173. // 2013年12月26日 星期四 下午05时27分03秒 CST
  174. public final static Date fullDateFormatStringToDate(String fullDateFormatString) {


  175. DateFormat fullDateFormat = DateFormat.getDateTimeInstance(
  176. DateFormat.FULL, DateFormat.FULL);
  177. Date date = null;
  178. // 文本格式String 转为 Date
  179. try {
  180. date = fullDateFormat.parse(fullDateFormatString);
  181. } catch (ParseException e) {
  182. // TODO Auto-generated catch block
  183. e.printStackTrace();
  184. }
  185. return date;
  186. }


  187. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值