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

来源:互联网 发布:双色球软件哪个好 编辑:程序博客网 时间:2024/05/29 15:37

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

关键字: java 字符串 时间 格式

通过长时间的对时间不同格式的转换,自己觉得其实就是对毫秒数的操作
Java代码 复制代码
  1. System.currentTimeMillis()  

得到从1970年1月1日到现在的毫秒数,就可以转换成各种的形式。

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


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

自己做了一个例子:

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