java字符串日期格式化,在Java中将字符串日期格式化为其他格式

博客内容讲述了在处理JSON日期格式时遇到的问题,即如何将`2017-05-23T06:25:50`格式的日期字符串转换为特定格式。作者提供了一个尝试的代码,但遇到了显示问题,TextView未显示转换后的日期,并且无法记录日志。解决方案是使用`SimpleDateFormat`进行两次转换,首先从原始格式解析日期,然后将其格式化为所需的显示格式。转换后的日期可以正确设置到TextView中。
摘要由CSDN通过智能技术生成

I am trying to format a JSON which has a date key in the format:

date: "2017-05-23T06:25:50"

My current attempt is the following:

private String formatDate(String date) {

SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy");

String formattedDate = "";

try {

formattedDate = format.format(format.parse(String.valueOf(date)));

} catch (ParseException e) {

e.printStackTrace();

}

return formattedDate;

}

But in this occasion, the result isn't shown in my app, the TextView is invisible and I couldn't log anything.

I really tried other solutions, but no success for me. I don't know if it's because the incoming value is a string.

解决方案

Use this code to format your `2017-05-23T06:25:50'

String strDate = "2017-05-23T06:25:50";

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

Date convertedDate = new Date();

try {

convertedDate = dateFormat.parse(strDate);

SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");

String finalDateString = sdfnewformat.format(convertedDate);

} catch (ParseException e) {

e.printStackTrace();

}

The converted finalDateString set to your textView

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值