将字符串转换为日期和时间为am / pm格式

编程入门 行业动态 更新时间:2024-10-28 09:12:54
本文介绍了将字符串转换为日期和时间为am / pm格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

string:2014-04-25 17:03:13

使用SimpleDateFormat的

是否足够格式化? 或否则我会转移到任何新的API?

Date date = new Date(string) ; DateFormat dateFormat = new SimpleDateFormat(yyyy-MM-dd); out.println(dateFormat.format(date));

我的预期结果是(印度地区):

日期:25-04-2014 时间:05:03 PM

解决方案

记住 Date 对象没有固有的格式,你需要两个 DateFormat 对象生成您寻求的结果 - 一个解析,另一个格式化:

String input = 2014-04-25 17:03:13 DateFormat inputFormat = new SimpleDateFormat(yyyy-MM-dd HH:mm:ss); DateFormat outputFormat = new SimpleDateFormat('Date Date''dd-MM-yyyy\\\'Time:'KK:mm a); System.out.println(outputFormat.format(inputFormat.parse(input)));

输出:

code>日期:25-04-2014 时间:05:03 PM

请注意使用格式中引用的序列,例如'Date:',在格式模式中被视为文字。

string : 2014-04-25 17:03:13

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

解决方案

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\n'Time : 'KK:mm a"); System.out.println(outputFormat.format(inputFormat.parse(input)));

Output:

Date : 25-04-2014 Time : 05:03 PM

Note the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.

更多推荐

将字符串转换为日期和时间为am / pm格式

本文发布于:2023-05-30 15:31:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/371050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间为   转换为   字符串   日期   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!