Java解析日期字符串返回错误的月份 [英] Java parse date string returns wrong month

查看:69
本文介绍了Java解析日期字符串返回错误的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解析日期字符串,但是月份却错误,为什么?

I try to parse a date string, but get wrong month, why?

new SimpleDateFormat("yyyy-MM-DD", Locale.US).parse("2018-03-08")

为什么它将月份返回为Jan?

请检查屏幕截图:

推荐答案

这是由于您用作"yyyy-MM-DD" 的格式.解析器将按以下顺序进行解析:

This is due to the format you used as "yyyy-MM-DD". The parser will parse in the sequence way:

  • 您的输入值为"2018-03-08"
  • yyyy -将进入2018年
  • MM -将进入3月
  • Your input value is "2018-03-08"
  • yyyy - will bring to the year 2018
  • MM - will bring to the month MARCH

但是什么是 DD ?这是从年初开始的天数.所以在这里,它又回到了今年(2018年)的第8个 天,也就是1月8日的 .

But what is DD? It's the number of the days from the beginning of the year. So here it moved back to 8th day on this year (2018) which means January 8th.

这就是为什么您看到的是1月而不是3月.

That's why you are seeing January instead of March.

这篇关于Java解析日期字符串返回错误的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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