java中的简单日期格式化程序问题

发布于 2024-09-10 15:50:26 字数 112 浏览 3 评论 0 原文

我想解析以下类型的日期:

2010-07-13T17:27:00.000Z

如何使用 java 中的简单日期格式化程序来完成它?使用什么格式?

I want to parse date of following type:

2010-07-13T17:27:00.000Z

How can i do it using simple date formatter in java? what format is to be used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以可爱出名 2024-09-17 15:50:26
  1. 查看 javadocs SimpleDateFormat
  2. 创建此类的实例,在构造函数中使用适当的 String
  3. 调用其 parse 方法,传入问题中的字符串
  4. ???
  5. 利润!

(您可能会注意到,我实际上并没有给您格式字符串。这是一个“授之以鱼”的答案。如果您在计算出具体您需要使用的内容时遇到问题一个特定的部分,然后随意详细说明,说明您尝试过什么以及为什么它不起作用,但现在听起来您还没有达到尝试任何特定格式字符串的程度。Javadocs 写得相当好。并包含您需要的一切。能够从文档中提取信息对于程序员来说是一项非常重要的技能,我不会剥夺您改进它的机会。)

  1. Take a look at the javadocs of SimpleDateFormat.
  2. Create an instance of this class, using the appropriate String in the constructor
  3. Call its parse method, passing in the String in your question
  4. ???
  5. Profit!

(You may notice that I'm not actually giving you the format string. This is a "teach a man to fish" answer. If you have problems working out specifically what you'd need to use for a particular section, then feel free to elaborate, stating what you tried and why it didn't work. But right now it sounds like you haven't got to the point of attempting any specific format strings. The Javadocs are reasonably well-written and contain everything you need. Being able to extract information from documentation is a massively important skill for a programmer and I'm not going to rob you of a chance to improve on it.)

温暖的光 2024-09-17 15:50:26

该代码应类似于以下代码。

对于日期字符串“2010-07-13T17:27:00.000Z”,您可以尝试以下格式“yyyy-MM-dd'T'hh:mm:ss.S'Z' ”。

我假设日期字符串中的“T”和“Z”只是常量/分隔符。

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestMain {

    public static void main(String[] args) throws Exception{

        String fromDateTime = "2010-12-01 00:01:23";
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
        Date date = null;

        date = format.parse(fromDateTime);
        //What ever you want to manipulate of this date object
        //... 

    }
}

编辑:添加适当的类、方法和注释使其成为一个完整的程序。感谢@Andrzej Doyle 的评论。
编辑:从演示程序中删除抛出 IOException。感谢@BalusC。
编辑:重新阅读评论,得到@BalusC的完整含义:)

The code should look like the following code.

For your date string "2010-07-13T17:27:00.000Z" you may try this format "yyyy-MM-dd'T'hh:mm:ss.S'Z'".

I assume the 'T' and 'Z' in your date string is constant/separator only.

import java.text.SimpleDateFormat;
import java.util.Date;

public class TestMain {

    public static void main(String[] args) throws Exception{

        String fromDateTime = "2010-12-01 00:01:23";
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
        Date date = null;

        date = format.parse(fromDateTime);
        //What ever you want to manipulate of this date object
        //... 

    }
}

EDIT: add proper class, method & comment to make it a complete program. Thanks for comment from @Andrzej Doyle.
EDIT: remove throws IOException from the demo program. Thanks for @BalusC.
EDIT: re-read the comment, got the full meaning of @BalusC :)

迷迭香的记忆 2024-09-17 15:50:26

日期时间字符串 2010-07-13T17:27:00.000Z 符合 ISO 8601 日期时间模式。在此字符串中,T 是日期和时间的分隔符,Z 代表 Zulu,它指定 UTC(即时区偏移量 +00:00 小时)。解析此日期时间字符串所需的格式如下:

yyyy-MM-dd'T'HH:mm:ss.SSSX 

文档页面

请注意,旧版日期时间 API(java.util 日期时间类型及其格式化 API SimpleDateFormat)已过时且容易出错。建议完全停止使用它们并切换到 java.time,即 现代日期时间 API*

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String args[]) throws ParseException {
        String strDateTime = "2010-07-13T17:27:00.000Z";

        Instant instant = Instant.parse(strDateTime);
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);

        System.out.println(instant);
        System.out.println(zdt);
        System.out.println(odt);

        // #################Using legacy API####################
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        Date date = sdf.parse(strDateTime);
        // Default format
        System.out.println(date);
        // Custom format
        sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        System.out.println(sdf.format(date));
        // #####################################################
    }
}

输出:

2010-07-13T17:27:00Z
2010-07-13T17:27Z
2010-07-13T17:27Z
Tue Jul 13 18:27:00 BST 2010
2010-07-13T17:27:00.000Z

请注意,现代日期时间中的默认模式遵循 ISO 8601 标准,因此您不需要 DateTimeFormatter (现代日期时间中 SimpleDateFormat 的对应部分API)显式地解析 ISO 8601 日期时间字符串。通过Trail: Date Time<了解有关现代日期时间 API 的更多信息/a>

这里需要提到的另一点是 java.util.Date 对象不是像 现代日期时间类型;相反,它表示自称为“纪元”的标准基准时间(即 1970 年 1 月 1 日 00:00:00 GMT(或 UTC))以来的毫秒数。当您打印 java.util.Date 对象时,其 toString 方法返回 JVM 时区中的日期时间(根据该毫秒值计算)。如果您需要打印不同时区的日期时间,则需要将时区设置为 SimpleDateFormat 并从中获取格式化字符串。


* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API如何在Android项目中使用ThreeTenABP

The date-time string, 2010-07-13T17:27:00.000Z complies with ISO 8601 date-time pattern. In this string, T is a separator for date and time and Z stands for Zulu which specifies UTC (i.e. a timezone offset of +00:00 hours). The format required to parse this date-time string is as follows:

yyyy-MM-dd'T'HH:mm:ss.SSSX 

Learn more about the pattern at the documentation page.

Note that the legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time, the modern date-time API* .

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    public static void main(String args[]) throws ParseException {
        String strDateTime = "2010-07-13T17:27:00.000Z";

        Instant instant = Instant.parse(strDateTime);
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);

        System.out.println(instant);
        System.out.println(zdt);
        System.out.println(odt);

        // #################Using legacy API####################
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
        Date date = sdf.parse(strDateTime);
        // Default format
        System.out.println(date);
        // Custom format
        sdf.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
        System.out.println(sdf.format(date));
        // #####################################################
    }
}

Output:

2010-07-13T17:27:00Z
2010-07-13T17:27Z
2010-07-13T17:27Z
Tue Jul 13 18:27:00 BST 2010
2010-07-13T17:27:00.000Z

Note that the default patterns in modern date-time follow ISO 8601 standard and there you do not need a DateTimeFormatter (the counterpart of SimpleDateFormat in the modern date-time API) explicitly in order to parse an ISO 8601 date-time string. Learn more about the modern date-time API from Trail: Date Time.

Another point important to be mentioned here is the java.util.Date object is not a real date-time object like the modern date-time types; rather, it represents the number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT (or UTC). When you print an object of java.util.Date, its toString method returns the date-time in the JVM's timezone, calculated from this milliseconds value. If you need to print the date-time in a different timezone, you will need to set the timezone to SimpleDateFormat and obtain the formatted string from it.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文