无法解析时间戳问题 java.time.format.DateTimeParseException: 无法在索引 0 处解析文本“9/25/2020, 12:46:00 PM" [英] Timestamp can't be parsed Issue java.time.format.DateTimeParseException: Text '9/25/2020, 12:46:00 PM' could not be parsed at index 0

查看:84
本文介绍了无法解析时间戳问题 java.time.format.DateTimeParseException: 无法在索引 0 处解析文本“9/25/2020, 12:46:00 PM"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在检查网页上的时间戳值之前和之后捕获时间,然后确认我的时间戳介于这些时间之间.但是,我正在努力以一种干净的方式将我的字符串时间戳转换为类似的格式.

I am attempting to capture the time before and after I check a timestamp value on a webpage and then confirm my timestamp falls in between those times. However, I'm struggling to convert my String timestamp into a comparable format in a clean way.

        Instant b4 = Instant.now();
                    
        //My code submites a file that then triggers the timestamp. I retrieve it as a string
        //exa string"9/25/2020, 11:03:18 AM"

        DateTimeFormatter dtf = DateTimeFormatter
                .ofPattern("MM/dd/yyyy, HH:mm:ss a")
                .withZone(ZoneId.systemDefault());
        Instant instantTimestamp = Instant.from(dtf.parse(timeStamp));

        Instant after = Instant.now();          

        if (instantTimestamp.isAfter(b4) && instantTimestamp.isBefore(after)) {
            testPass = true;
        }

        Assert.assertTrue(testPass);

我的错误:java.time.format.DateTimeParseException: 无法在索引 0 处解析文本9/25/2020, 12:46:00 PM"

My Error: java.time.format.DateTimeParseException: Text '9/25/2020, 12:46:00 PM' could not be parsed at index 0

推荐答案

月份的格式与其在字符串中的值不匹配.格式为MM,指定两位数,但值为9,为一位数.您可以使用单个字母表示月、日、年、小时、分钟、秒等,以容纳所有允许的数字位数.另外,我建议您以不区分大小写的方式解析它,以便可以容纳大写和小写(例如 AMam).

There is a mismatch in the format for the month and its value in the string. The format is MM which specifies two digits but the value is 9 which is a single digit. You can use single letters for month, day, year, hour, minute, seconds etc. to accommodate all allowable numbers of digits. Also, I suggest you parse it in a case-insensitive way so that upper and lower case (e.g. AM and am) both can be accommodated.

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // Given date-time string
        String dateTimeString = "9/25/2020, 12:46:00 PM";

        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                .parseCaseInsensitive()
                                .appendPattern("M/d/u, h:m:s a")
                                .toFormatter(Locale.ENGLISH);

        // Convert the given date-time string to LocalDateTime
        LocalDateTime ldt = LocalDateTime.parse(dateTimeString, dtf);
        System.out.println(ldt);

        // Convert to LocalDateTime Instant if required
        Instant instant = ldt.toInstant(ZoneOffset.UTC);
        System.out.println(instant);
    }
}

输出:

2020-09-25T12:46
2020-09-25T12:46:00Z

在线演示

如果此日期时间所属的时区与 UTC 不同,则通过 ZonedDateTime 将其转换为 Instant 例如

If the timezone to which this Date-Time belongs is different from UTC, convert it to Instant through ZonedDateTime e.g.

Instant instant = ldt.atZone(ZoneId.of("America/Los_Angeles")).toInstant();

Trail 了解有关现代日期时间 API 的更多信息:日期时间.

Learn more about the modern Date-Time API from Trail: Date Time.

这篇关于无法解析时间戳问题 java.time.format.DateTimeParseException: 无法在索引 0 处解析文本“9/25/2020, 12:46:00 PM"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆