简体   繁体   English

你如何解析2位数的年份日期?

[英]How do you parse 2-digit year date with time?

Sorry for bringing this up, but I don't see it. 很抱歉提起这件事,但我没有看到。 I have date+time in such format: 我有这样的格式的日期+时间:

dd-MM-yy HH:mm dd-MM-yy HH:mm

For example 例如

15-11-11 09:36 15-11-11 09:36

(at the time of writing, it indicates date 5 days ago). (在撰写本文时,它表示5天前的日期)。 When I try to run: 当我尝试运行时:

new java.text.SimpleDateFormat("dd-MM-yy HH:mm").parse("15-11-11 09:36")

I get exception: 我得到例外:

java.text.ParseException: Unparseable date: "15-11-11 09:36" java.text.ParseException:Unparseable date:“15-11-11 09:36”

At first I thought it is something with year (despite (-80,+20) boundary -- according to documentation). 起初我认为这是年份(尽管(-80,+ 20)边界 - 根据文件)。 Well, maybe, but when I remove time (from string and pattern), the date is recognized correctly. 好吧,也许,但是当我删除时间(来自字符串和模式)时,日期被正确识别。

Does anyone see what I don't see? 有谁看到我没看到的?

Update 更新

My system (Linux) short date format: 我的系统(Linux)短日期格式:

YYYY-MM-DD YYYY-MM-DD

long: 长:

WEEKDAY DD MONTH YYYY 每周DD月YYYY

and time: 和时间:

HH:MM:SS HH:MM:SS

However I thought the purpose of giving the pattern is to be locale independent. 但是我认为给出模式的目的是独立于语言环境。

Out of curiosity I added ":ss" to pattern and ":01" to string, still exception. 出于好奇,我将“:ss”添加到模式,将“:01”添加到字符串,仍然是例外。

I can just estimate but I think that you copy/pasted either format fragment or the date. 我可以估计,但我认为您复制/粘贴格式片段或日期。 And by mistake inserted different semicolon character in both cases. 并且在两种情况下都错误地插入了不同的分号字符。 The characters look the same but are different. 角色看起来相同但不同。

So, I suggest you to take your working example without time and type manually HH:mm and 10:36 . 所以,我建议你把你的工作示例不受时间和手动键入 HH:mm10:36 If it does not work, I am really sorry. 如果它不起作用,我真的很抱歉。 :(. If it does try first to change 10 to 09 . This is to be sure that it takes numbers with leading zeros. :(。如果确实首先尝试将10改为09这是为了确保它需要带有前导零的数字。

Try this 试试这个

DateFormat df = new SimpleDateFormat("dd-MM-yy HH:mm");  

System.out.println("Formatted date: " + df.format(date));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM