[英]How to get proper written date using SimpleDateFormat in Java
I have this:我有这个:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date result = dateFormat.parse(this.getCreatedTime());
Basically I want to convert a string like "2016-09-27T09:19:57Z" into something like "September 27, 2016 at 9:19 AM".基本上,我想将“2016-09-27T09:19:57Z”之类的字符串转换为“2016 年 9 月 27 日上午 9:19”之类的字符串。
If I use the code above I end up with a Date
object, but all the methods are deprecated.如果我使用上面的代码,我最终会得到一个
Date
对象,但所有方法都被弃用了。 So how do I achieve this?那么我该如何实现呢?
You can use DateFormat again as @Thomas wrote:您可以再次使用 DateFormat,因为@Thomas 写道:
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
Date inputDate = inputFormat.parse(this.getCreatedTime());
DateFormat outputFormat = new SimpleDateFormat("outputFormat");
String output = outputFormat.format(inputDate);
You should do research before posting any question here.您应该在此处发布任何问题之前进行研究。
Use this to get Date
from your required pattern使用它从您所需的模式中获取
Date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
Date date = null;
try {
date = format.parse(unformattedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Make new instance of your desired patter.制作您想要的模式的新实例。
format = new SimpleDateFormat("MMMM dd, yyyy 'at' HH:mm a", Locale.getDefault());
String formattedDate = format.format(date);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.