在Java中将字符串日期格式化为其他格式 [英] Formatting a String date into a different format in Java

查看:1369
本文介绍了在Java中将字符串日期格式化为其他格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试格式化具有date键的JSON格式:

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

日期:"2017-05-23T06:25:50"

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

我当前的尝试如下:

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;
}

但是在这种情况下,结果未显示在我的应用程序中,TextView是不可见的,我什么也无法记录.

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.

推荐答案

使用此代码格式化您的"2017-05-23T06:25:50"

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();
}

转换后的finalDateString设置为您的textView

这篇关于在Java中将字符串日期格式化为其他格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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