如何格式化给定的时间字符串并转换为 date/time 对象

How to format the given time string and convert to date/time object

我正在从休息服务中获取字符串形式的时间对象。我需要提取时间然后做一些时间 operation.The 给定时间字符串是“2015-06-16T14:58:48Z”。我尝试了下面的代码,将字符串转换为时间,但是得到的值不正确。

    String time = "2015-06-16T14:58:48Z";

    SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss'Z'", Locale.US);

    String dateInString = "2015-06-16T14:58:48Z";

    Date date = formatter.parse(dateInString);
    System.out.println("Original String : " + time);
    System.out.println("After converting to time : " + formatter.format(date));

我得到的输出如下: 原始字符串:2015-06-16T14:58:48Z 转换为时间后:2015-12-362T02:58:48Z

转换后的日期不知何故出错了value.Please建议mistake.Thanks。

将 SimpleDateFormat 更改为此..

SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ssX", Locale.US);

你的格式化字符串有几个错误:

  • Y表示周年,而不是,即y
  • D表示的日子。您应该使用 d,这意味着 .
  • 的日期
  • h 表示一天中的 12 小时制时间。由于您有 14,您应该使用 H,它处理 24 小时制。

总结一下:

SimpleDateFormat formatter = 
    new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US);

java.time

问题的根本原因是使用了错误的符号

  • Y(指定基于周的年份)而不是y(指定时代)
  • D(指定)而不是d(指定) =78=]).
  • h(指定 clock-hour-of-am-pm)而不是 H(指定 hour-当天).

查看documentation页面 了解有关这些符号的更多信息。

另请注意,旧版日期时间 API(java.util 日期时间类型及其格式 API、SimpleDateFormat)已过时且容易出错.建议完全停止使用,改用java.timemodern date-time API*.

使用现代API的解决方案:

现代日期时间 API 基于 ISO 8601 and does not require you to use a DateTimeFormatter object explicitly as long as the date-time string conforms to the ISO 8601 standards. Your date-time string conforms to ISO 8601 standards (or the default format used by OffsetDateTime#parse).

import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2015-06-16T14:58:48Z";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        System.out.println(odt);

        // ########################Extract time information########################
        LocalTime time = odt.toLocalTime();

        // You can also get it as time.getHour()
        // Extract other units in a similar way
        int hour = odt.getHour();

        // Also using time.format(DateTimeFormatter.ofPattern("a", Locale.ENGLISH));
        String amPm = odt.format(DateTimeFormatter.ofPattern("h a", Locale.ENGLISH));

        System.out.println(time);
        System.out.println(hour);
        System.out.println(amPm);
    }
}

输出:

2015-06-16T14:58:48Z
14:58:48
14
2 PM

注:

  1. 输出中的 Z 是零时区偏移量的 timezone designator。它代表祖鲁语并指定 Etc/UTC 时区(时区偏移量为 +00:00 小时)。
  2. 出于任何原因,如果您需要将OffsetDateTime的这个对象转换为java.util.Date的对象,您可以按如下方式进行:
    Date date = Date.from(odt.toInstant());

Trail: Date Time[=90= 中了解有关 modern date-time API* 的更多信息].


* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用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