java中字串轉換成時間格式總結

來源:互聯網
上載者:User

有這樣一個字串:"20070911121547",

轉換成時間格式:2007-09-11 12:15:47

Java代碼
  1. public class bb {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
  5. String dateString = "20071128175545";
  6. try {
  7. Date date = df.parse(dateString);
  8. System.out.println(df.format(date));
  9. } catch (Exception ex) {
  10. System.out.println(ex.getMessage());
  11. }
  12. }
  13. }
public class bb {        public static void main(String[] args) {                // TODO Auto-generated method stub                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");                String dateString = "20071128175545";                try {                        Date date = df.parse(dateString);                        System.out.println(df.format(date));                } catch (Exception ex) {                        System.out.println(ex.getMessage());                }        }}

時間無非就是字串類型轉向時間類型,或則時間類型轉向字串類型,還有就是前一個時間,後一個時間的處理等等

自己做了一個例子:

Java代碼
  1. package com.observe.monitoralarm.util;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /**
  7. * 時間協助類
  8. * @version $Id: DateUtil.java,v 1.1 2008/05/28 04:29:52 linan Exp $
  9. * @author LiNan
  10. */
  11. public class DateUtil {
  12. private Calendar calendar=Calendar.getInstance();
  13. /**
  14. * 得到當前的時間,時間格式yyyy-MM-dd
  15. * @return
  16. */
  17. public String getCurrentDate(){
  18. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  19. return sdf.format(new Date());
  20. }
  21. /**
  22. * 得到當前的時間,自訂時間格式
  23. * y 年 M 月 d 日 H 時 m 分 s 秒
  24. * @param dateFormat 輸出顯示的時間格式
  25. * @return
  26. */
  27. public String getCurrentDate(String dateFormat){
  28. SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
  29. return sdf.format(new Date());
  30. }
  31. /**
  32. * 日期格式化,預設日期格式yyyy-MM-dd
  33. * @param date
  34. * @return
  35. */
  36. public String getFormatDate(Date date){
  37. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  38. return sdf.format(date);
  39. }
  40. /**
  41. * 日期格式化,自訂輸出日期格式
  42. * @param date
  43. * @return
  44. */
  45. public String getFormatDate(Date date,String dateFormat){
  46. SimpleDateFormat sdf=new SimpleDateFormat(dateFormat);
  47. return sdf.format(date);
  48. }
  49. /**
  50. * 返回當前日期的前一個時間日期,amount為正數 目前時間後的時間 為負數 目前時間前的時間
  51. * 預設日期格式yyyy-MM-dd
  52. * @param field 日曆欄位
  53. * y 年 M 月 d 日 H 時 m 分 s 秒
  54. * @param amount 數量
  55. * @return 一個日期
  56. */
  57. public String getPreDate(String field,int amount){
  58. calendar.setTime(new Date());
  59. if(field!=null&&!field.equals("")){
  60. if(field.equals("y")){
  61. calendar.add(calendar.YEAR, amount);
  62. }else if(field.equals("M")){
  63. calendar.add(calendar.MONTH, amount);
  64. }else if(field.equals("d")){
  65. calendar.add(calendar.DAY_OF_MONTH, amount);
  66. }else if(field.equals("H")){
  67. calendar.add(calendar.HOUR, amount);
  68. }
  69. }else{
  70. return null;
  71. }
  72. return getFormatDate(calendar.getTime());
  73. }
  74. /**
  75. * 某一個日期的前一個日期
  76. * @param d,某一個日期
  77. * @param field 日曆欄位
  78. * y 年 M 月 d 日 H 時 m 分 s 秒
  79. * @param amount 數量
  80. * @return 一個日期
  81. */
  82. public String getPreDate(Date d,String field,int amount){
  83. calendar.setTime(d);
  84. if(field!=null&&!field.equals("")){
  85. if(field.equals("y")){
  86. calendar.add(calendar.YEAR, amount);
  87. }else if(field.equals("M")){
  88. calendar.add(calendar.MONTH, amount);
  89. }else if(field.equals("d")){
  90. calendar.add(calendar.DAY_OF_MONTH, amount);
  91. }else if(field.equals("H")){
  92. calendar.add(calendar.HOUR, amount);
  93. }
  94. }else{
  95. return null;
  96. }
  97. return getFormatDate(calendar.getTime());
  98. }
  99. /**
  100. * 某一個時間的前一個時間
  101. * @param date
  102. * @return
  103. * @throws ParseException
  104. */
  105. public String getPreDate(String date) throws ParseException{
  106. Date d=new SimpleDateFormat().parse(date);
  107. String preD=getPreDate(d,"d",1);
  108. Date preDate=new SimpleDateFormat().parse(preD);
  109. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  110. return sdf.format(preDate);
  111. }
  112. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.