首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java LocalDateTime在解析时丢弃零?

Java LocalDateTime在解析时丢弃零?
EN

Stack Overflow用户
提问于 2022-10-20 17:31:59
回答 1查看 41关注 0票数 -2

我将把一个String解析成LocalDateTime,然后反过来进行分析。但是解析器正在删除时间中秒部分的零。

这个问题与this other question有关(不一样)。但是,我再次发布它,因为接受的响应提到使用DateTimeFormatter,我已经使用了它,您可以在下面的代码中看到,我在ISO_8601_PATTERN变量中指定秒。

我不知道我错过了什么,或者我做错了什么。

代码语言:javascript
运行
复制
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main
{
    private final static String DEFAULT_PATTERN = "yyyy/MM/dd";
    private final static String ISO_8601_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
    private final static String UTC_ZONE_ID = "UTC";
    
    public static void main(String[] args) {
        String stringDateTime = "2022-03-14T21:38:00Z";
        LocalDateTime dateTime = fromIso8601(stringDateTime);
        stringDateTime = toIso8601(dateTime);
        System.out.println(stringDateTime);
        // Prints: 2022-03-14T21:38Z
        // Should print: 2022-03-14T21:38:00Z
    }
    
    public static LocalDateTime fromIso8601(String dateTimeString) {
        return LocalDateTime.parse(dateTimeString, 
            DateTimeFormatter.ofPattern(ISO_8601_PATTERN).withZone(ZoneId.of(UTC_ZONE_ID)));
    }

    public static String toIso8601(LocalDateTime dateTime) {
        return dateTime.atZone(ZoneOffset.UTC).toString();
    }
    
}
EN

回答 1

Stack Overflow用户

发布于 2022-10-20 17:40:03

使用DateTimeFormatter而不是toString:

代码语言:javascript
运行
复制
public static String toIso8601(LocalDateTime dateTime) {
    return DateTimeFormatter.ofPattern(ISO_8601_PATTERN).format(dateTime);
    
}

toString()方法不添加零值:

代码语言:javascript
运行
复制
//-----------------------------------------------------------------------
/**
 * Outputs this time as a {@code String}, such as {@code 10:15}.
 * <p>
 * The output will be one of the following ISO-8601 formats:
 * <ul>
 * <li>{@code HH:mm}</li>
 * <li>{@code HH:mm:ss}</li>
 * <li>{@code HH:mm:ss.SSS}</li>
 * <li>{@code HH:mm:ss.SSSSSS}</li>
 * <li>{@code HH:mm:ss.SSSSSSSSS}</li>
 * </ul>
 * The format used will be the shortest that outputs the full value of
 * the time where the omitted parts are implied to be zero.
 *
 * @return a string representation of this time, not null
 */
@Override
public String toString() {
    StringBuilder buf = new StringBuilder(18);
    int hourValue = hour;
    int minuteValue = minute;
    int secondValue = second;
    int nanoValue = nano;
    buf.append(hourValue < 10 ? "0" : "").append(hourValue)
        .append(minuteValue < 10 ? ":0" : ":").append(minuteValue);
    if (secondValue > 0 || nanoValue > 0) {
        buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
        if (nanoValue > 0) {
            buf.append('.');
            if (nanoValue % 1000_000 == 0) {
                buf.append(Integer.toString((nanoValue / 1000_000) + 1000).substring(1));
            } else if (nanoValue % 1000 == 0) {
                buf.append(Integer.toString((nanoValue / 1000) + 1000_000).substring(1));
            } else {
                buf.append(Integer.toString((nanoValue) + 1000_000_000).substring(1));
            }
        }
    }
    return buf.toString();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74144063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档