定义一个取得当前系统日期时间的工具类

开发中经常需要取得日期,而且每次取得日期的时候代码都会重复,所以既然是重复的代码就可以将其定义成一个类,以方便重复调用,但是在操作的时候有一点特别需要注意:如果月会是9月,则应该显示09,但是如果是09的话,则数字肯定会忽略到0。

1.Calendar 类操作 (取得日期时间、中文的日期时间、时间戳)

除了取出日期之外,取得时间戳也是一种比较常见的操作,例如:以下日期:2009-01-16 11:25:34.953

时间戳:20090116112534953

 

import java.util.* ;	// 导入需要的工具包

class DateTime{		// 以后直接通过此类就可以取得日期时间
	private Calendar calendar = null ;// 声明一个Calendar对象,取得时间
	
	public DateTime(){			// 构造方法中直接实例化对象
		this.calendar = new GregorianCalendar() ;	
	}
	
       // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
	public String getDate(){	
		// 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
		StringBuffer buf = new StringBuffer() ;
		buf.append(calendar.get(Calendar.YEAR)).append("-") ;// 增加年
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("-") ;// 增加月
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append(" ") ;// 取得日
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append(":") ;// 取得时
		buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append(":") ;
		buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append(".") ;
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ;
		return buf.toString() ;
	}
	
	public String getDateComplete(){		// 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
		// 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
		StringBuffer buf = new StringBuffer() ;
		buf.append(calendar.get(Calendar.YEAR)).append("年") ;	// 增加年
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)).append("月") ;// 增加月
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)).append("日") ;// 取得日
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)).append("时") ;// 取得时
		buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)).append("分") ;	// 取得分
		buf.append(this.addZero(calendar.get(Calendar.SECOND),2)).append("秒") ;	// 取得秒
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)).append("毫秒") ;	// 取得毫秒
		return buf.toString() ;
	}
	
	public String getTimeStamp(){		// 得到的是一个时间戳
		// 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能
		StringBuffer buf = new StringBuffer() ;
		buf.append(calendar.get(Calendar.YEAR)) ;	// 增加年
		buf.append(this.addZero(calendar.get(Calendar.MONTH)+1,2)) ;	// 增加月
		buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH),2)) ;	// 取得日
		buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY),2)) ;	// 取得时
		buf.append(this.addZero(calendar.get(Calendar.MINUTE),2)) ;		// 取得分
		buf.append(this.addZero(calendar.get(Calendar.SECOND),2));		// 取得秒
		buf.append(this.addZero(calendar.get(Calendar.MILLISECOND),3)) ;	 // 取得毫秒
		return buf.toString() ;
	}
	
	// 考虑到日期中存在前导0,所以在此处加上补零的方法
	private String addZero(int num,int len){
		StringBuffer s = new StringBuffer() ;
		s.append(num) ;
		while(s.length()<len){	// 如果长度不足,则继续补0
			s.insert(0,"0") ;	// 在第一个位置处补0
		}
		return s.toString() ;
	}
}

public class DateDemo{
	public static void main(String args[]){
		DateTime dt = new DateTime() ;
		System.out.println("系统日期:"+dt.getDate()) ;
		System.out.println("中文日期:"+dt.getDateComplete()) ;
		System.out.println("时间戳:"+dt.getTimeStamp()) ;
	}
}

 输出结果:

 

系统日期:2013-03-11 17:23:43.174
中文日期:2013年03月11日17时23分43秒174毫秒
时间戳:20130311172343174

 2.基于SimpleDateFormat类操作

 

 

java.util.Date 已经就是一个完整的日期了,SimpleDateFormat 类中存在一个方法,可以针对于Date重新格式化,那么如果现在将一个表示当前日期的 date 对象通过 SimpleDateFormat 类指定好的模板进行相关的格式化操作的话,那么取得时间就非常的方便了。

import java.util.* ;	// 导入需要的工具包
import java.text.* ;	// 导入SimpleDateFormat所在的包

class DateTime{		// 以后直接通过此类就可以取得日期时间
	private SimpleDateFormat sdf = null ;	// 声明SimpleDateFormat对象
	
	// 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
	public String getDate(){		
		this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
		return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
	}
	
	// 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
	public String getDateComplete(){		
		this.sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒") ;
		return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
	}
	
	// 得到的是一个时间戳
	public String getTimeStamp(){		
		this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS") ;
		return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
	}
}
public class DateDemo{
	public static void main(String args[]){
		DateTime dt = new DateTime() ;
		System.out.println("系统日期:"+dt.getDate()) ;
		System.out.println("中文日期:"+dt.getDateComplete()) ;
		System.out.println("时间戳:"+dt.getTimeStamp()) ;
	}
}

 输出结果:

系统日期:2013-03-11 17:26:51.564
中文日期:2013年03月11日17时26分51秒564毫秒
时间戳:20130311172651565

 总结:

 

通过代码可以发现,直接使用SimpleDateFormat 类取得时间会比使用Calendar 类更方便,而且不用去增加补零操作,所以在开发中如果需要取得一个日期的话,则基本上都是使用 SimpleDateFormat 类进行操作,以上的操作代码为取得日期的一个简单做法。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值