Java 如何在Android中将“yyyy-MM-dd'T'HH:mm:ss.SSSXXX”日期格式解析为简单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50368493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:35:44  来源:igfitidea点击:

How to parse "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" date format to simple in Android?

javaandroidjsondatedatetime-parsing

提问by picKit

How do I can parse this date 2018-01-09T11:11:02.0+03:00to dd.MM.yyyy hh:mmformat in Android?

如何解析此日期2018-01-09T11:11:02.0+03:00dd.MM.yyyy hh:mm在 Android 中格式化?

And what does Tbetween 09and 11mean? Thanks.

又是什么T之间0911是什么意思?谢谢。

I don't know how the back-end developer got this format. I am using Java.

不知道后端开发者是怎么弄到这个格式的。我正在使用 Java。

采纳答案by E.Abdel

If you are using java, you can use SimpeDateFormatwith patterns:

如果您使用的是 java,则可以使用SimpeDateFormat模式:

        String date = "2018-01-09T11:11:02.0+03:00";
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        SimpleDateFormat output = new SimpleDateFormat("dd.MM.yyyy hh:mm");
        Date d = null;
        try {
            d = dateformat.parse(date /*your date as String*/);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formattedDate = output.format(d);

        Log.d("Date format", "output date :" + formattedDate);

The output is :

输出是:

D/Date?format: output date :09.01.2018 09:11

D/Date? 格式:输出日期:09.01.2018 09:11

EDIT :Thanks to @OleV.V., for API > 26, or using ThreeTenABPwe can use

编辑:感谢@OleV.V.,对于 API > 26,或者使用ThreeTenABP我们可以使用

DateTimeFormatter, we can do something like that

DateTimeFormatter,我们可以做这样的事情

    String date = "2018-01-09T11:11:02.0+03:00";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern("dd.MM.yyyy hh:mm");
    LocalDate parsedDate = LocalDate.parse(date, formatter);

    String formattedDate = formatterOut.format(parsedDate);
    Log.d("Date format", "output date :" + formattedDate);

回答by Anthony Cannon

You can do this with SimpleDateFormat.

你可以用SimpleDateFormat做到这一点。

Here is a tested example in Java:

这是一个在 Java 中经过测试的示例:

String dateString = "2018-01-09T11:11:02.0+03:00";

String inPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
String outPattern = "dd.MM.yyyy hh:mm";

SimpleDateFormat inFormat = new SimpleDateFormat(inPattern, Locale.getDefault());
SimpleDateFormat outFormat = new SimpleDateFormat(outPattern, Locale.getDefault());

try {
    Date inDate = inFormat.parse(dateString);
    String outDate = outFormat.format(inDate);
    Log.e("TEST", outDate);
} catch (ParseException e) {
    e.printStackTrace();
}

Here is a tested example in Kotlin:

这是Kotlin 中经过测试的示例:

val dateString = "2018-01-09T11:11:02.0+03:00"

val inPattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
val outPattern = "dd.MM.yyyy hh:mm"

val inFormat = SimpleDateFormat(inPattern, Locale.getDefault())
val outFormat = SimpleDateFormat(outPattern, Locale.getDefault())

val inDate = inFormat.parse(dateString)
val outDate = outFormat.format(inDate)

Log.e("TEST", outDate)

回答by Ole V.V.

    DateTimeFormatter desiredFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

    String backendDateTimeString = "2018-01-09T11:11:02.0+03:00";
    OffsetDateTime dateTime = OffsetDateTime.parse(backendDateTimeString);
    String presentationDateTimeString = dateTime.format(desiredFormatter);
    System.out.println(presentationDateTimeString);

This prints:

这打印:

09.01.2018 11:11

09.01.2018 11:11

Please note: I am using uppercase HHin the format pattern string. This indicates hour of day from 00 through 23. In the question you used lowercase hh, which in a format pattern string means hour with AM or PM from 01 through 12, so 00:33 would come out as 12:33 and 15:47 as 03:47. I didn't think you intended this.

请注意:我HH在格式模式字符串中使用大写。这表示从 00 到 23 的一天中的小时。在您使用小写的问题中hh,在格式模式字符串中表示小时与上午或下午从 01 到 12,因此 00:33 将作为 12:33 和 15:47 出现03:47。我认为你不是故意的。

The format that your backend developer got, 2018-01-09T11:11:02.0+03:00, is ISO 8601. It's widespread, and it's the international standard, so it's good that s/he got that. The funny Tin the middle indicates the start of the time part to separate it from the date part. The one-arg OffsetDateTime.parsemethod parses ISO 8601, which is why we didn't need any formatter for parsing. OffsetDateTimeand DateTimeFormatterare classes from java.time, the modern Java date and time API.

您的后端开发人员获得的格式2018-01-09T11:11:02.0+03:00是 ISO 8601。它很普遍,而且是国际标准,所以他/她能获得它是件好事。T中间的滑稽表示时间部分的开始,以将其与日期部分分开。one-argOffsetDateTime.parse方法解析 ISO 8601,这就是为什么我们不需要任何格式化程序来解析。OffsetDateTime并且DateTimeFormatter是来自java.time现代 Java 日期和时间 API 的类。

Question: Can I use java.time on Android?

问题:我可以在 Android 上使用 java.time 吗?

Yes, java.timeworks nicely on older and newer Android devices. It just requires at least Java 6.

是的,java.time在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6

  • In Java 8 and later and on newer Android devices (API level 26 and up) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bpwith subpackages (my code was tested with these imports).
  • 在 Java 8 及更高版本以及更新的 Android 设备(API 级别 26 及更高版本)中,现代 API 是内置的。
  • 在 Java 6 和 7 中获得 ThreeTen Backport,即新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从org.threeten.bp子包中导入日期和时间类(我的代码已使用这些导入进行了测试)。

Links

链接