Date,DateFormat,Calendar

一:目前的jdk推荐使用时间类:Calendar

 

①获取Calendar

 

 

Calendar calendar = Calendar.getInstance();
 

②时间数与毫秒数的相互转换

 

 

Long l = calendar.getTimeInMillis();

calendar.setTimeInMillis(l);
 

③获取时间

 

 

String nowStr = calendar.get(calendar.YEAR)+"年"+calendar.get(calendar.MONTH)+"月"+calendar.get(calendar.DAY_OF_MONTH)+"日 "+
			calendar.get(calendar.HOUR_OF_DAY)+"时"+calendar.get(calendar.MINUTE)+"分"+calendar.get(calendar.SECOND)+"秒";
System.out.println(nowStr);

 

④设置时间(两种方式)

 

 

//Date参数市值
Date d = new Date();calendar.setTime(d);
//毫秒参数设置
Long ll = 123456789;calendar.setTime(ll);
 

⑤时间的修改

 

 

calendar.add(calender.HORE, 4);
calendar.set(calendar.DAY_OF_MONTH, 6);
 

二:已过时的Date类,但很多借口使用它作为参数,将它的大部分方法都废弃了,辅助以DateFormat来完成功能

 

①获取Date

 

Date date = new Date();
 

②Date与SimpleDateFormat

 

SimpleDateFormat sp1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat sp2 = new SimpleDateFormat("yyyy年MM月dd日 hh分mm时ss秒");

Date d = new Date();

System.out.println(sp1.format(d));
System.out.println(sp2.format(d));
Date d1 = sp1.parse("2011-01-01 23:24:13");
 

 

以上内容自己总结,欢迎指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值