简体   繁体   English

将 ISO8601 TZ 格式的字符串转换为日期

[英]Converting String in ISO8601 T-Z format to Date

Possible Solution: Convert Java Date into another Time as Date format可能的解决方案: 将 Java 日期转换为另一种时间作为日期格式

I went through it but does not get my answer.我经历了它,但没有得到我的答案。

I have a string " 2013-07-17T03:58:00.000Z " and I want to convert it into date of the same form which we get while making a new Date().Date d=new Date();我有一个字符串“ 2013-07-17T03:58:00.000Z ”,我想将其转换为与我们在创建 new Date().Date d=new Date() 时得到的相同形式的日期;

The time should be in IST Zone - Asia/Kolkata时间应该在IST区-亚洲/加尔各答

Thus the date for the string above should be因此上面字符串的日期应该是

Wed Jul 17 12:05:16 IST 2013 //Whatever Time as per Indian Standard GMT+0530 2013 年 7 月 17 日星期三 12:05:16 IST //任何时间根据印度标准 GMT+0530

String s="2013-07-17T03:58:00.000Z";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); 
TimeZone tx=TimeZone.getTimeZone("Asia/Kolkata");
formatter.setTimeZone(tx);
d= (Date)formatter.parse(s);

Use calendar for timezones.时区使用日历。

TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
Calendar cal = Calendar.getInstance(tz);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
sdf.setCalendar(cal);
cal.setTime(sdf.parse("2013-07-17T03:58:00.000Z"));
Date date = cal.getTime();

For this however I'd recommend Joda Time as it has better functions for this situation.但是,为此我建议使用Joda Time,因为它在这种情况下具有更好的功能。 For JodaTime you can do something like this:对于 JodaTime,您可以执行以下操作:

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
DateTime dt = dtf.parseDateTime("2013-07-17T03:58:00.000Z");
Date date = dt.toDate();

A Date doesn't have any time zone.日期没有任何时区。 If you want to know what the string representation of the date is in the indian time zone, then use another SimpleDateFormat, with its timezone set to Indian Standard, and format the date with this new SimpleDateFormat.如果您想知道印度时区中日期的字符串表示形式,请使用另一个 SimpleDateFormat,其时区设置为印度标准,并使用此新 SimpleDateFormat 格式化日期。

EDIT: code sample:编辑:代码示例:

String s = "2013-07-17T03:58:00.000Z";
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
Date d = formatter.parse(s);

System.out.println("Formatted Date in current time zone = " + formatter.format(d));

TimeZone tx=TimeZone.getTimeZone("Asia/Calcutta");
formatter.setTimeZone(tx);
System.out.println("Formatted date in IST = " + formatter.format(d));

Output (current time zone is Paris - GMT+2):输出(当前时区是巴黎 - GMT+2):

Formatted Date in current time zone = 2013-07-17T05:58:00.000+02
Formatted date in IST = 2013-07-17T09:28:00.000+05

java.time时间

I should like to provide the modern answer.我想提供现代答案。 And give you a suggestion: rather than reproducing what Date.toString() would have given you, better to use the built-in format for users in the relevant locale:并给你一个建议:与其复制Date.toString()会给你的东西,不如使用相关语言环境中的用户的内置格式:

    String isoString = "2013-07-17T03:58:00.000Z";
    String humanReadable = Instant.parse(isoString)
            .atZone(userTimeZone)
            .format(localizedFormatter);
    System.out.println(humanReadable);

On my computer this prints在我的电脑上打印

17 July 2013 at 09.28.00 IST

My snippet uses a couple of constants我的代码段使用了几个常量

private static final ZoneId userTimeZone = ZoneId.of("Asia/Kolkata");
private static final DateTimeFormatter localizedFormatter = DateTimeFormatter
        .ofLocalizedDateTime(FormatStyle.LONG)
        .withLocale(Locale.getDefault(Locale.Category.FORMAT));

The withLocale call is redundant when we just pass the default format locale.当我们只传递默认格式区域设置时, withLocale调用是多余的。 I put it in so you have a place to provide an explicit locale, should your users require a different one.我把它放进去是为了让你有一个地方提供一个明确的语言环境,如果你的用户需要不同的语言环境。 It also has the nice advantage of making explicit that the result depends on locale, in case someone reading your code didn't think of that.它还具有明确表明结果取决于语言环境的好处,以防有人阅读您的代码时没有想到这一点。

I am using java.time , the modern Java date and time API.我正在使用java.time ,现代 Java 日期和时间 API。 It is so much nicer to work with than the outdated Date , TimeZone and the notoriously troublesome DateFormat and SimpleDateFormat .它比过时的DateTimeZone和臭名昭著的DateFormatSimpleDateFormat得多。 Your string conforms to the ISO 8601 standard, a format that the modern classes parse as their default, that is, without any explicit formatter.您的字符串符合 ISO 8601 标准,现代类将其解析为默认格式,即没有任何显式格式化程序。

Same format as Date gave you与 Date 给你的格式相同

Of course you can have the same format as the old-fashioned Date would have given you if you so require.当然,如果您需要,您可以使用与老式Date相同的格式。 Just substitute the formatter above with this one:只需用这个替换上面的格式化程序:

private static final DateTimeFormatter asUtilDateFormatter 
        = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ROOT);

Now we get:现在我们得到:

Wed Jul 17 09:28:00 IST 2013

Link to tutorial链接到教程

Read more about using java.time in the Oracle tutorial and/or find other resources out there.Oracle 教程中阅读有关使用java.time更多信息和/或查找其他资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM