如何将字符串转换为 java 中的日期 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

How to convert String to Date "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" in java

我有一个字符串日期。例如:"2020-02-21 16:36:30.072",我想将其转换为日期 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"。 (即 2020-02-21T16:36:30.072+05:30

怎么样?你能帮帮我吗?

您需要首先将 String 转换为更 "malleable" 的格式 - 可以代表 date/time 的某种方式,通过它您可以根据自己的需要生成不同的格式需要

既然是 2020 年,你应该从 java.time.* API 开始。

String input = "2020-02-21 16:36:30.072";
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
LocalDateTime ldt = LocalDateTime.parse(input, inputFormatter);

好的,现在,您的部分要求是拥有时区,因此您需要将 LocalDateTime 转换为 ZonedDateTime,从技术上讲,您可以在单步,但是很好示范

ZonedDateTime zdt = ldt.atZone(ZoneId.systemDefault());

DateTimeFormatter outputFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String output = outputFormatter.format(zdt);

System.out.println(input);
System.out.println(output);

这将输出

2020-02-21 16:36:30.072
2020-02-21T16:36:30.072+11:00

我住在澳大利亚,所以我的时区是 +10(夏令时是 +1)。如果你愿意,你可以指定一个特定的时区,这只是为了演示(我不会费心去弄清楚 530+ 时区;))

tl;博士

这是单行本。

LocalDateTime
.parse(
    "2020-02-21 16:36:30.072".replace( " " , "T" )
)                                                   // Returns a `LocalDateTime` object. *Not* a moment, *not* a point on the timeline. Just a date and a time-of-day, nothing more. Lacks context of a time zone or offset-from-UTC.
.atZone(                                            // Lending context to our `LocalDateTime` object to determine a moment by assigning a time zone.
    ZoneId.of( "Asia/Kolkata" )                     // Currently using an offset of five and a half hours ahead of UTC.
)                                                   // Returns a `ZonedDateTime` object.
.format(                                            // Generates text representing the value of the `ZonedDateTime` object.
    DateTimeFormatter.ISO_OFFSET_DATE_TIME          // Pre-defined formatter. No need to specify your own formatting pattern. Your desired format complies with the ISO 8601 standard.
)                                                   // Returns a `String`. 

看到这个 code run live at IdeOne.com

2020-02-21T16:36:30.072+05:30

详情

正确且详细。我会采取一些捷径,并专门解决您想要的 +05:30 偏移量。

我们可以将您的输入解析为 LocalDateTime,只需将中间的 SPACE 字符替换为大写字母 T

String input = "2020-02-21 16:36:30.072".replace( " " , "T" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;

一个LocalDateTime不是代表一个时刻,是不是时间轴上的一个点。它缺少时区或与 UTC 的偏移量的上下文。

您想要的 +05:30 偏移量目前仅在两个时区使用:

  • Asia/Colombo(斯里兰卡)
  • Asia/Kolkata(印度)

选择属于你的。

ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;

应用到您的 LocalDateTime 以确定时刻,结果是 ZonedDateTime

ZonedDateTime zdt = ldt.atZone( z ) ;

以标准 ISO 8601 格式生成字符串,明智地扩展以在方括号中附加区域名称。

String output = zdt.toString() ;

如果您真的只想要没有时区的偏移量,请记住,您的数据的读者无法确定您指的是斯里兰卡时间还是印度时间。如果您坚持,请使用另一个答案中所示的预定义 DateTimeFormatter 对象。


导入java.util.Date;

public class 测试 {

public static void main(String[] args) {
    String s0="2020-02-21 16:36:30.072";
    Date date=new Date();
    String sd=s0.split(" ")[0];
    date.setDate(Integer.parseInt(sd.split("-")[2]));
    date.setMonth(Integer.parseInt(sd.split("-")[1])-1);
    date.setYear(Integer.parseInt(sd.split("-")[0])-1900);
    String st=s0.split(" ")[1];
    date.setSeconds((int) Double.parseDouble(st.split(":")[2]));
    date.setMinutes(Integer.parseInt(st.split(":")[1]));
    date.setHours(Integer.parseInt(st.split(":")[0]));
    System.out.print(date);
}

}