gpt4 book ai didi

允许 -/或 .作为日期内的分隔符

转载 作者:搜寻专家 更新时间:2023-10-31 08:24:56 27 4
gpt4 key购买 nike

解析可以采用以下格式之一的日期的最佳方法是什么

 "dd-MM-yyyy HH:mm"
"dd/MM/yyyy HH:mm"
"dd.MM.yyyy HH:mm"

无需创建 3 个 SimpleDateFormat 并按每个。

谢谢

最佳答案

将源字符串“调整”为规范格式可能是最简单的方法:

if (text.length() == 16)
{
if ((text.charAt(2) == '/' && text.charAt(5) == '/') ||
(text.charAt(2) == '.' && text.charAt(5) == '.'))
{
text = text.substring(0, 2) + "-" + text.substring(3, 5)
+ "-" + text.substring(6);
}
}

然后使用带“-”的格式字符串。

请注意,这是非常具体的,只能完全替换您感兴趣的字符,以避免不必要的副作用。

关于允许 -/或 .作为日期内的分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1443430/

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