Java日期格式(转载)

通过长时间的对时间不同格式的转换,自己觉得其实就是对毫秒数的操作

Java代码
  1. System.currentTimeMillis()  
    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. }  
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.   
  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.      * @return  
  112.      * @throws ParseException   
  113.      */   
  114.     public  String getPreDate(String date)  throws  ParseException{  
  115.         Date d=new  SimpleDateFormat().parse(date);  
  116.         String preD=getPreDate(d,"d" , 1 );  
  117.         Date preDate=new  SimpleDateFormat().parse(preD);  
  118.         SimpleDateFormat sdf = new  SimpleDateFormat( "yyyy-MM-dd" );  
  119.         return  sdf.format(preDate);  
  120.     }  
  121.       
  122.       

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值