开发中经常需要取得日期,而且每次取得日期的时候代码都会重复,所以既然是重复的代码就可以将其定义成一个类,以方便重复调用,但是在操作的时候有一点特别需要注意:
如果月会是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() ;
}
public String getDate(){ // 得到的是一个日期:格式为: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)) ;
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 DateDemo06{
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()) ;
}
};
2.基于SimpleDate类操作
java.util.Date 已经就是一个完整的日期了,SimpleDateFormat 类中存在一个方法,可以针对于Date重新格式化,那么如果现在将一个表示当前日期的 date 对象通过 SimpleDateFormat 类指定好的模板进行相关的格式化操作的话,那么取得时间就非常的方便了。
import java.util.* ; // 导入需要的工具包
import java.text.* ; // 导入SimpleDateFormat所在的包
class DateTime{ // 以后直接通过此类就可以取得日期时间
private SimpleDateFormat sdf = null ; // 声明SimpleDateFormat对象
public String getDate(){ // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS
this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;
return this.sdf.format(new Date()) ;// 将当前日期进行格式化操作
}
public String getDateComplete(){ // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒
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 DateDemo07{
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()) ;
}
};
总结:
通过代码可以发现,直接使用SimpleDateFormat 类取得时间会比使用Calendar 类更方便,而且不用去增加补零操作,所以在开发中如果需要取得一个日期的话,则基本上都是使用 SimpleDateFormat 类进行操作,以上的操作代码为取得日期的一个简单做法。