Data类
世界标准时间:格林尼治时间/格林威治时间(Greenwich Mean Time) 中国标准时间,世界标准时间+8小时 (东8区) 1秒=1000毫秒 1毫秒=1000微秒 计算机起始时间:1970年1月1日00:00:00 System.currentTimeMillis 从1970年0:00:00到此时的毫秒数 getTime()方法获取当前时间的毫秒值 setTime()方法设置当前日期时间值 无参构造就是把当前时间封装成一个Date对象 有参构造就是把从时间原点开始,过了指定毫秒的时间,封装成一个Date对象,需要考虑时差
//创建一个Date对象表示本机当前时间
Date date=new Date();
System.out.println(date);//Thu Sep 08 19:09:40 CST 2022
/*从计算机的时间原点开始,过了指定毫秒的时间
从时间原点开始过了0毫秒
因为现在在东八区,所以加了八小时*/
Date date1 = new Date(0l);//long类型的
System.out.println(date1);//Thu Jan 01 08:00:00 CST 1970
//表示1970年1月1日10点
Date date2=new Date(1000*60*60*2l);//因为在东八区。所以加俩小时即可
System.out.println(date2);//Thu Jan 01 10:00:00 CST 1970
//当前时间的date对象
Date date=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(date);//Thu Sep 08 19:35:15 CST 2022
System.out.println(sdf);//java.text.SimpleDateFormat@4f76f1a0
//format:Date到String进行格式化
System.out.println(sdf.format(date));//2022-09-08 19:37:47
//parse:String到Date进行解析
String str="2022-09-08";
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf1.parse(str));//Thu Sep 08 00:00:00 CST 2022
本文含有隐藏内容,请 开通VIP 后查看