将字符串转换为日期和时间为am / pm格式 [英] convert string to date and time as am/pm format

查看:1100
本文介绍了将字符串转换为日期和时间为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格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆