使用SimpleDateFormat格式化日期

import java.text.* ;
import java.util.* ;
public class DateDemo05{
	public static void main(String args[]) throws Exception{
		String strDate = "2008-10-19 10:11:30" ;
		// 准备第一个模板,从字符串中提取出日期数字
		String pat = "yyyy-MM-dd HH:mm:ss" ;
		SimpleDateFormat sdf = new SimpleDateFormat(pat) ;		// 实例化模板对象
		Date d = sdf.parse(strDate) ;	// 将给定的字符串中的日期提取出来
		System.out.println(d) ;	// 输出Date对象
		System.out.println(sdf.format(d)) ;	// 将日期变为新的格式
	}
};
 

输出结果:

Sun Oct 19 10:11:30 CST 20082008-10-19 10:11:30

使用SimpleDateFormat格式化日期。 以上程序实现String向Date的操作转型

import java.text.* ;
import java.util.* ;
 class DateTime{
	//声明日期格式化操作对象,直接对new Date()进行实例化
	private SimpleDateFormat simpleDateFormat=null;
	//得到完整日期,格式为:yyyy-MM-dd HH:mm:ss:SSS
	public String getDate() {
		this.simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		return this.simpleDateFormat.format(new Date());
	}
	//得到完整的日期,格式为:yyyy年MM月dd日HH时mm分ss秒SSS毫秒
	public String getDateComplete() {
		this.simpleDateFormat= new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒SSS毫秒");
		return this.simpleDateFormat.format(new Date());
	}
	//得到时间戳:yyyyMMddHHmmssSSS
	public String getTimeStamp() {
		this.simpleDateFormat= new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return this.simpleDateFormat.format(new Date());
	}
};

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

	}
}

输出结果

系统日期:2016-03-03 14:09:23:292
中文日期:2016年03月03日14时09分23秒293毫秒
时间戳:20160303140923293


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值