1、如何获取系统当前时间
pubulic static void main(String[] args)
{
Date nowTime=new Date();//获取精确到毫秒的系统当前时间
// java已经对java.util.Date中的ToString方法进行了重写,因此打印nowTime方法
可以直接输出时间日期字符串。
2、如何将Date数据转化为String类型数据
//可以对系统的输出时间进行格式化,将日期时间数据Date格式数据直接转换成
具有一定格式的String字符串。
// SimpleDateFormat是java.txt包下的专门进行日期数据的格式化
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss sss");//
也可以是任何你想要的 形式,但是里面的规则必须是用上图的符号来代表。
String nowTimeStr=sdf.format(nowTime);//
这里是间nowTime里面的时间日期数据按照sdf制定的标准格式进行时间格式化
3、将String数据转换为Date数据
String time ="2000-09-09 09:09:09 888";
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss sss")
Date datatime=sdf2.Parse(time);//parse的作用是解析
System.out.println(dateTime);//Fri Aug 09 09:09:09 CST 2000
4、获取自1970年1月1日 0:0:0 000到现在有多少毫秒
long nowTimeMilis=System.CurrentTimeMillis()
System.out.println(nowTimeMilis);//打印自1970年1月1日 0:0:0 000到现在有多少毫秒
long begin=System.CurrentTimeMillis()
程序体
long begin=System.CurrentTimeMillis()
long time=end-begin;
System.out.println(time);可以计算一个程序运行耗费了多少毫秒。
}