convert string to date and time as am/pm format(将字符串转换为上午/下午格式的日期和时间)
问题描述
string : 2014-04-25 17:03:13
使用 SimpleDateFormat 格式化就够了?或者否则我会转向任何新的 API?
using SimpleDateFormat is enough to format? or otherwise i will shift to any new API?
Date date = new Date(string);
DateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd");
out.println( dateFormat.format (date));
我的预期结果是(印度区):
My expected result is (India zone):
Date : 25-04-2014
Time : 05:03 PM
推荐答案
记住 Date
对象没有固有格式,你需要两个 DateFormat
对象来产生你想要的结果seek - 一个解析,另一个格式化:
Remembering that Date
objects have no inherent format, you need two DateFormat
objects to produce the result you seek - one to parse and another to format:
String input = "2014-04-25 17:03:13";
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("'Date : 'dd-MM-yyyy
'Time : 'KK:mm a");
System.out.println(outputFormat.format(inputFormat.parse(input)));
输出:
Date : 25-04-2014
Time : 05:03 PM
注意格式中引用的序列的使用,例如 "'Date : '"
,它在格式模式中被视为文字.
Note the use of quoted sequences in the format, such a "'Date : '"
, which is treated as a literal within the format pattern.
这篇关于将字符串转换为上午/下午格式的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将字符串转换为上午/下午格式的日期和时间


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01