如何将时间戳从yyyy-MM-ddThh:mm:ss:SSSZ格式转换为MM / dd / yyyy hh:mm:ss.SSS格式?从ISO8601到UTC [英] How can I convert a timestamp from yyyy-MM-ddThh:mm:ss:SSSZ format to MM/dd/yyyy hh:mm:ss.SSS format? From ISO8601 to UTC

查看:3231
本文介绍了如何将时间戳从yyyy-MM-ddThh:mm:ss:SSSZ格式转换为MM / dd / yyyy hh:mm:ss.SSS格式?从ISO8601到UTC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将时间戳记2011-03-10T11:54:30.207Z转换为10/03/2011 11:54:30.207。我该怎么做?我想将ISO8601格式转换为UTC,然后UTC应该是位置感知。请帮助

I want to convert the timestamp 2011-03-10T11:54:30.207Z to 10/03/2011 11:54:30.207. How can I do this? I want to convert ISO8601 format to UTC and then that UTC should be location aware. Please help

String str_date="2011-03-10T11:54:30.207Z";
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
date = (Date)formatter.parse(str_date);
System.out.println("output: " +date );

异常:java.text.ParseException:Unparseable date:2011-03-10T11:54:30.207 Z

Exception :java.text.ParseException: Unparseable date: "2011-03-10T11:54:30.207Z"

推荐答案

首先,您需要注意UTC不是格式,它是一个时区,有效。所以从ISO8601转换为UTC并不是一个概念。

Firstly, you need to be aware that UTC isn't a format, it's a time zone, effectively. So "converting from ISO8601 to UTC" doesn't really make sense as a concept.

然而,这里是一个使用Joda Time的示例程序,它将文本解析成code> DateTime 然后格式化。我猜测您可能想要使用的 格式 - 您还没有提供足够的信息,说明您想要做的更多的事情。您可能还想考虑时区...要在指定时刻显示本地时间?如果是这样,你需要制定用户的时区并进行适当的转换。

However, here's a sample program using Joda Time which parses the text into a DateTime and then formats it. I've guessed at a format you may want to use - you haven't really provided enough information about what you're trying to do to say more than that. You may also want to consider time zones... do you want to display the local time at the specified instant? If so, you'll need to work out the user's time zone and convert appropriately.

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        String text = "2011-03-10T11:54:30.207Z";
        DateTimeFormatter parser = ISODateTimeFormat.dateTime();
        DateTime dt = parser.parseDateTime(text);

        DateTimeFormatter formatter = DateTimeFormat.mediumDateTime();
        System.out.println(formatter.print(dt));
    }
}

这篇关于如何将时间戳从yyyy-MM-ddThh:mm:ss:SSSZ格式转换为MM / dd / yyyy hh:mm:ss.SSS格式?从ISO8601到UTC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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