Java-将yyyy-MM-dd'T'HH:mm:ssZ转换为可读的dd-MM-yyyy [英] Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy

查看:327
本文介绍了Java-将yyyy-MM-dd'T'HH:mm:ssZ转换为可读的dd-MM-yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以以下方式输入字符串:

I will have a String input in the style of:


yyyy-MM-dd'T'HH:mm:ssZ

yyyy-MM-dd'T'HH:mm:ssZ

是否可以将此String转换为日期,然后将其解析为可读的 dd-MM -yyyy 日期对象?

Is it possible to convert this String into a date, and after, parsing it into a readable dd-MM-yyyy Date Object?

推荐答案

是。它可以分为两部分,如下所示:

Yes. It can be done in two parts as follows:


  1. 解析您的 String Date 对象

SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date dt = sd1.parse(myString);


  • Date 对象的格式设置为所需格式

  • Format the Date object to desirable format

    SimpleDateFormat sd2 = new SimpleDateFormat("yyyy-MM-dd");
    String newDate = sd2.format(dt);
    System.out.println(newDate);
    


  • 您将不得不使用两个不同的 SimpleDateFormat ,因为两种日期格式不同。

    You will have to use two different SimpleDateFormat since the two date formats are different.

    输入:

    2015-01-12T10:02:00+0530
    

    输出:

    2015-01-12
    

    这篇关于Java-将yyyy-MM-dd'T'HH:mm:ssZ转换为可读的dd-MM-yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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