java - 如何根据 DateFormat.SHORT 和语言环境显示 Date 对象的时间?

标签 java date java-7

我使用的是 Java 7,我需要显示 Java Date 对象的时间部分。

我注意到 DateFormat 根据 the pageSHORT 常量该页面有以下描述:

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/

相关文章:

mysql - 在 SQL 中使用日期

java - JDK-7 SwingWorker 死锁?

java - 尝试读取 DTD 文件的非验证 DocumentBuilder

java-7 - javax.net.ssl.SSLException : Received fatal alert: unexpected_message in Java7 异常

复杂字符之间的 Java 正则表达式文本

java - 将 CSV 文件转换为 Java 对象 (POJO) 并将其发送到 ActiveMQ 队列

linux - 如果列中的模式匹配,则提取标题

mysql - 将字符串 dd/MM/yyyy 日期转换为 java.sql.date yyyy-MM-dd

linux - ./jdk-6u34-linux-i586.bin : No such file or directory

java - Java 中的 While 循环具有多个条件