当前日期& Java中的时间 [英] Current Date & Time in Java
本文介绍了当前日期& Java中的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望当前日期和时间采用以下格式:
日期:YYYYMMDD
时间: HHMMSS
我尝试了以下
DateFormat dateFormat = new SimpleDateFormat yyyyMMdd);
//使用Date()
获取当前日期时间Date date = new Date();
System.out.println(dateFormat.format(date));
//使用Calendar()获取当前日期时间
日历cal = Calendar.getInstance();
System.out.println(new Date()。getTime());
通过这个我得到所需的日期输出,但时间是这样来的1341837848290。 p>
预期是HHMMSS。
解决方案
使用 format()
System.out.println(new SimpleDateFormat(HH:mm:SS)。format(new Date ));
日期实例没有任何属性来保存自定义格式,因此您需要格式化日期实例到您的自定义格式的字符串HH:mm:SS(请参阅 API文档获取更多详细信息)
查看
I want the current date and time in the following format :
Date :YYYYMMDD
Time : HHMMSS
I tried the following
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//get current date time with Date()
Date date = new Date();
System.out.println(dateFormat.format(date));
//get current date time with Calendar()
Calendar cal = Calendar.getInstance();
System.out.println(new Date().getTime());
By this I am getting the desired date output but the time is coming in this way 1341837848290.
The expected is HHMMSS.
解决方案
Use format()
System.out.println(new SimpleDateFormat("HH:mm:SS").format(new Date()));
Date instance doesn't have any property to hold custom format, So you need to format the date instance to String with your custom format HH:mm:SS (See API doc for more detail)
See
这篇关于当前日期& Java中的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文