java中字符串转换成时间格式总结

有这样一个字符串:"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. }



时间无非就是字符串类型转向时间类型,或则时间类型转向字符串类型,还有就是前一个时间,后一个时间的处理等等

自己做了一个例子:

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. }
原文地址:https://www.cnblogs.com/pony/p/1364782.html