gpt4 book ai didi

SimpleDateFormat giving error for the same input for Time(SimpleDateFormat为相同的时间输入提供错误)

转载 作者:bug小助手 更新时间:2023-10-25 23:28:19 28 4
gpt4 key购买 nike



I am really not able to understand why SimpleDateFormat is throwing error for the very similar inputs.

我真的不能理解为什么SimpleDateFormat对非常相似的输入抛出错误。


I am passing two values : "05:20 AM" & "05:50 AM"

我传递了两个值:“05:20 AM”和“05:50 AM”


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormateDate {

public static void main(String[] args) throws ParseException {
String date_s = "05:20 AM";
//String date_s = "05:50 AM";
String date_ss;

SimpleDateFormat dt = new SimpleDateFormat("hh:mm a");
Date date = dt.parse(date_s);


// *** same for the format String below

System.out.println(dt.format(date));
}
}

The first value throws error :

第一个值引发错误:


Exception in thread "main" java.text.ParseException: Unparseable date: "05:20 AM"

and the second value one prints output : 05:50 AM

第二个值1打印输出:05:50 AM


NOTE: Let me be clear that it is not a typo error or copy/paste error. Both strings are coming from my backend in API and I was not able to figure out why the first one throws an error.

注意:让我澄清一下,这不是打字错误或复制/粘贴错误。这两个字符串都来自我在API中的后端,我不知道为什么第一个会抛出错误。


更多回答

Do not delete this Question. This use of non-breaking space character is an important issue, as some localizations recently adopted the use of that character to keep the AM/PM next to time-of-day. I don’t recall details at the moment, but will soon try to find my sources again.

请勿删除此问题。这种不间断空格字符的使用是一个重要的问题,因为一些本地化最近采用了该字符来将AM/PM保持在一天中的下一个时间。目前我记不起细节,但很快就会再次尝试寻找我的消息来源。

优秀答案推荐

You've got non-ASCII unicode symbols in your input strings. Fortunately, you copy/pasted and, and pasted those too.

您的输入字符串中有非ASCII Unicode符号。幸运的是,您也复制/粘贴和粘贴了这些内容。


date_s is the following sequence of characters:

DATE_S是以下字符序列:



  • 0

  • 5

  • :

  • 2

  • 0


so far, so good.

到现在为止还好。



  • 0xE280AF

  • A

  • M


What the heck are those 3 bytes before AM?

AM之前的3个字节到底是什么?


It's the UTF-8 encoding of the unicode symbol U+202F: The narrow no-break space.

它是Unicode符号U+202F的UTF-8编码:狭窄的不间断空格。


Probably a browser where someone used   instead of a space.

可能是有人使用 ;而不是空格的浏览器。


SimpleDateFormat is obsolete. Do not use it; use the stuff in java.time instead (DateTimeFormatter). But, if you insist on using SDF, try:

SimpleDateFormat已过时。不要使用它;而是使用java.time(DateTimeForMatter)中的东西。但是,如果您坚持使用SDF,请尝试:


String date_s = "05:20 AM".replaceAll("\\p{Z}", " ");

That weird incantation is using regexp to say: "replace all characters that fit in the unicode Z set (which is all whitespace codepoint) with an actual space". That actual space is something SDF can handle; those fancy unicode spaces aren't.

这个奇怪的咒语是使用regexp来表示:“用实际的空格替换适合Unicode Z集(全是空白码点)的所有字符”。SDF可以处理实际的空间;而那些花哨的Unicode空间则不能。


更多回答

@HovercraftFullOfEels answers to questions that are caused by typos are inherently unuseful, as they hinder the effort to clean up the question.

@HoverCraftFullOfEels对打字错误导致的问题的回答本质上是无用的,因为它们阻碍了清理问题的努力。

@KarlKnechtel I usually agree with you on this except when I don't. Usually a typo question is a low quality affair that often attracts low quality answers, but occasionally you see a pretty decent answer that shows decent sleuthing and excellent exposition, sometimes enough to raise the utility of the question. This may be such a case. But as always, I encourage you to vote as you see fit, and I'll do the same.

@KarlKnechtel在这一点上我通常同意你的观点,除非我不同意。通常情况下,打字错误的问题是一个低质量的问题,通常会吸引低质量的答案,但偶尔你会看到一个相当像样的答案,显示出体面的侦察和出色的阐述,有时足以提高问题的实用性。这可能就是这样的情况。但一如既往,我鼓励你们按照你们认为合适的方式投票,我也会这样做。

@KarlKnechtel This is no mere typo. The non-breaking space has recently been adopted for some localizations of date-time formats. This choice makes sense as the AM/PM should indeed be kept with the time-of-day, not separated by a line break. I just read this week about this new adoption, likely related to the Unicode CLDR, but I cannot recall exactly. I’ll investigate tomorrow.

@卡尔·克内克特尔这不仅仅是打字错误。最近,日期-时间格式的一些本地化使用了不间断空格。这一选择是有意义的,因为AM/PM确实应该与一天中的时间保持一致,而不是用换行符分隔。我这周刚刚读到这一新的采用,可能与Unicode CLDR有关,但我记不清确切的情况了。我明天会调查的。

Let me be clear to you guys thats its not type error. Both the data is coming from my backend and I was unable to figure the difference in both the data. So I do want to try this answer

让我向你们澄清,这不是打字错误。这两个数据都来自我的后端,而我无法计算出这两个数据之间的差异。所以我真的想试试这个答案

Thank you so much for the answer. It Worked. I spent hours and hours figuring out the difference between those two strings.

非常感谢你的回答。啊,真灵。我花了好几个小时弄清楚这两根弦之间的区别。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com