久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

將時間戳解析為 LocalDateTime

Parsing timestamp as LocalDateTime(將時間戳解析為 LocalDateTime)
本文介紹了將時間戳解析為 LocalDateTime的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我想將 yyyy-MM-dd'T'HH:mm:ss 形式的時間戳解析為 LocalDateTime.這樣做時,如果它們是 00,它會去除秒數(shù).

I want to parse a timestamp in the form of yyyy-MM-dd'T'HH:mm:ss as LocalDateTime. When doing so, it strips the seconds if they are 00.

如此處所述,我需要使用自定義格式化程序

As described here, I need to use a custom formatter

LocalDateTime date = LocalDateTime.parse("2008-10-02T12:30:00");
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

String dateString = date.toString();
String dateFormatted = date.format(f);

System.out.println(dateString); // 2008-10-02T12:30

這個可行,但它返回一個 String:

This one works, but it returns a String:

System.out.println(dateFormatted); // 2008-10-02T12:30:00

當(dāng)我將字符串解析為 LocalDateTime 時,它會再次剝離 00:

When I parse the string to LocalDateTime it strips the 00 again:

LocalDateTime dateLDT = LocalDateTime.parse(dateFormatted, f);
System.out.println(dateLDT); // 2008-10-02T12:30

那么如何將日期解析為 LocalDateTime,而不是 String,并在末尾保留 00?

So how can I parse a date as LocalDateTime, instead of String, and keep the 00 at the end?

推薦答案

你應(yīng)該期待兩者之間的輸出差異

You should expect a difference in output between

LocalDateTime dateLDT = LocalDateTime.parse(dateFormatted, f);
System.out.println(dateLDT);

還有

System.out.println(dateLDT.format(f)) //or f.format(dateLDT)

System.out.println(dateLDT); 打印 dateLDT.toString() 的值,預(yù)計(jì)不會產(chǎn)生與您的模式相同的輸出.

System.out.println(dateLDT); prints the value of dateLDT.toString(), which is not expected to produce the same output as your pattern.

當(dāng)您查看 LocalDateTime.toString() 時,您會看到它將時間部分委托給 LocalTime.toString(),它有條件地打印秒數(shù):

When you look at LocalDateTime.toString(), you'll see that it delegates the time part to LocalTime.toString(), which prints seconds conditionally:

public String toString() {
    ...
    if (secondValue > 0 || nanoValue > 0) {
        buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
        ...
        }
    }
    return buf.toString();
}

如果它的值為0,它只是省略秒字段.

It simply omits the seconds field if its value is 0.

在這種情況下,如果您必須確定輸出/輸入格式,您需要始終使用 DateTimeFormatter 來格式化您的日期.

What you need to do in this case is always use a DateTimeFormatter to format your date if you have to be certain about the output/input format.

這篇關(guān)于將時間戳解析為 LocalDateTime的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉(zhuǎn)換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉(zhuǎn)換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當(dāng)前星期幾的值)
主站蜘蛛池模板: 欧美video | 精品一区二区三区中文字幕 | 日本成人二区 | 亚洲国产精品久久 | 国产 欧美 日韩 一区 | 精品国产免费人成在线观看 | 国产一区不卡 | 视频一区二区在线观看 | 精品日韩 | av中文字幕网| 亚洲视频在线免费观看 | 欧美精品1区2区 | 亚洲在线一区 | 国产中文字幕在线观看 | 国产真实精品久久二三区 | 久久精品免费一区二区三 | 一区二区av | 亚洲精品视频在线看 | 国产黄色在线观看 | 国产精品毛片无码 | 亚洲一区中文字幕 | 欧美日韩国产一区二区三区 | 成人福利在线 | 韩国久久 | 久草免费福利 | 日韩亚洲视频 | 国产一区三区在线 | 日本网站免费在线观看 | 国产高清在线 | 成人蜜桃av | 亚洲小说图片 | 亚洲国产精品99久久久久久久久 | 欧美一级大片免费看 | 九九久久久| 国产高清视频 | 日本久久久一区二区三区 | 国产亚洲精品一区二区三区 | 超碰在线97国产 | 成人av一区 | 日本成人福利视频 | 一区二区视频在线 |