使用SimpleDateFormat.parse时错误的月份 [英] Getting wrong month when using SimpleDateFormat.parse

查看:172
本文介绍了使用SimpleDateFormat.parse时错误的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中有一个非常奇怪的问题。在这里你可以看到 String birthday Log 来查看它:

In my programm there is a very strange problem. Here you can see String birthday and Log to check it:

birthday = String.valueOf(birthYear) + "-" + String.valueOf(birthMonth) + "-" + String.valueOf(birthDay);
Log.i(TAG, "Birthday: " + birthday)

然后我把它放到SimpleDateFormat并使用 Log 进行检查:

Then I put it to SimpleDateFormat and check it with the Log:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");

Date birthDate = sdf.parse(birthday);
Log.i(TAG, "Birth date  : " + birthDate);

然后在 Logcat 我有:

I/App﹕ Birthday: 1999-10-15
I/App﹕ Birth date: Fri Jan 15 00:10:00 GMT+04:00 1999

所以你在日期看到的是 Jan ,但在字符串中它是 10 所以日期应如下所示:

So as you see in the date it is Jan, but in the String it is 10 so date should look like:

Fri Nov 15 00:10:00 GMT+04:00 1999

我的错误在哪里?

PS我认为我的问题与使用SimpleDateFormat.parse()时获取错误数据

P.S I think my question is somehow connected with Getting wrong data when using SimpleDateFormat.parse()

推荐答案

使用:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

month <使用 MM / strong>, mm 分钟,如文档 ...

Use MM for month, mm for minutes as stated by documentation...

如果要打印日期以特定格式,你应该使用:

If you want to print a Date in a specific format, you should use:

 sdf.format(birthday)

或其他 SimpleDateFormat 如果你想pring它以不同的格式......

or another SimpleDateFormat if you want to pring it in a different format...

这篇关于使用SimpleDateFormat.parse时错误的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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