Java自学--Date类 格式化输出时间

import java.text.* ;
import java.util.* ;
public class DateDemo05{
	public static void main(String args[]){
		String strDate = "2008-10-19 10:11:30.345" ;
		// 准备第一个模板,从字符串中提取出日期数字
		String pat1 = "yyyy-MM-dd HH:mm:ss.SSS" ;
		// 准备第二个模板,将提取后的日期数字变为指定的格式
		String pat2 = "yyyy年MM月dd日 HH时mm分ss秒SSS毫秒" ;
		SimpleDateFormat sdf1 = new SimpleDateFormat(pat1) ;		// 实例化模板对象
		SimpleDateFormat sdf2 = new SimpleDateFormat(pat2) ;		// 实例化模板对象
		Date d = null ;
		try{
			d = sdf1.parse(strDate) ;	// 将给定的字符串中的日期提取出来
		}catch(Exception e){			// 如果提供的字符串格式有错误,则进行异常处理
			e.printStackTrace() ;		// 打印异常信息
		}
		System.out.println(sdf2.format(d)) ;	// 将日期变为新的格式
	}
};

格式化输出时间



实例一:基于Calendar类操作取得系统时间

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()) ;
	}
};


实例二: 基于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()) ;
	}
};





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值