我使用的是 Java 7,我需要显示 Java Date
对象的时间部分。
我注意到 DateFormat
根据 the page 有 SHORT
常量该页面有以下描述:
SHORT
is completely numeric, such as 12.13.52 or 3:30pm
我的问题是如何仅显示时间部分(例如“3:30pm”)。以下是我所拥有的,它只显示日期部分(例如 2/14/15):
DateFormat f = DateFormat.getDateInstance(DateFormat.SHORT, A_Locale_object);
SimpleDateFormat sf = (SimpleDateFormat) f;
sf.format(new Date());
最佳答案
如果要显示的是时间部分,则需要调用getTimeInstance()
,而不是getDateInstance()
。
您应该学会自己找到这种答案,只需阅读the javadoc即可。 :
Use getDateInstance to get the normal date format for that country. There are other static factory methods available. Use getTimeInstance to get the time format for that country. Use getDateTimeInstance to get a date and time format.
关于java - 如何根据 DateFormat.SHORT 和语言环境显示 Date 对象的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28513069/