本文介绍了将字符串转换为日期和时间为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:13using 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 PMNote the use of quoted sequences in the format, such a "'Date : '", which is treated as a literal within the format pattern.
更多推荐
将字符串转换为日期和时间为am / pm格式
发布评论