系统输出时间格式转换


1、正常获取系统时间输出格式:

java代码:

System.out.println(new java.util.Date());  

输出:Wed Jul 18 17:08:58 CST 2018

 

2、修改其格式

(1)用toLocaleString()方法(已过时,不过好像还能用)

java代码:

System.out.println(new java.util.Date().toLocaleString());  

输出:2018-7-18 17:08:58

(2)过时的toLocaleString()方法由DateFormat.format(Date date)取代

java代码:

Date date=new Date();
        
DateFormat ddf=DateFormat.getDateInstance();
System.out.println("日期:"+ddf.format(date));
System.out.println("====================");
        
DateFormat dtf=DateFormat.getTimeInstance();
System.out.println("时间:"+dtf.format(date));
System.out.println("====================");
        
DateFormat ddtf=DateFormat.getDateTimeInstance();
System.out.println("日期时间:"+ddtf.format(date));
System.out.println("====================");
        
SimpleDateFormat sdf=(SimpleDateFormat)DateFormat.getDateTimeInstance();
        System.out.println("日期时间:"+sdf.format(date));
        System.out.println("====================");
//这种可以指定格式

SimpleDateFormat sy1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

 System.out.println("日期时间:"+sy1.format(date));

输出:

日期:2018-7-18
====================
时间:17:08:58
====================
日期时间:2018-7-18 17:08:58
====================
日期时间:2018-7-18 17:08:58
====================

日期时间:2018-7-18 17:08:58

 

本方法适用于Windows系统。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM