java 获取当前时间的三种方法 (三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuforeverlove/article/details/81708385

为了自己方便,先记录下来 

1.Date获取

Date d = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(dateFormat.format(d));

2.Calendar 获取

Calendar calendar= Calendar.getInstance();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(dateFormat.format(calendar.getTime()));

3.Calendar 获取时间,分别获取年月日时分秒

Calendar cal=Calendar.getInstance();      
int year=cal.get(Calendar.YEAR);      
int month=cal.get(Calendar.MONTH)+1;      
int date=cal.get(Calendar.DATE);      
int hour=cal.get(Calendar.HOUR_OF_DAY);      
int mi=cal.get(Calendar.MINUTE);      
int second=cal.get(Calendar.SECOND);      
System.out.println("现在时刻是"+year+"年"+month+"月"+date+"日"+hour+"时"+mi+"分"+second+"秒");

猜你喜欢

转载自blog.csdn.net/xuforeverlove/article/details/81708385