如何获得的月和日在android系统? [英] How to get month and day in android?
问题描述
我需要得到月份和日期为:01 01月02日,二月......和01月的第一天,等等。我试过这样:
I need to get month and day as :01 for January, 02 for February... and 01 for first day of month, etc... I tried this :
String dd = c.get(Calendar.YEAR) + "-"
+ c.get(Calendar.MONTH)
+ "-" + c.get(Calendar.DAY_OF_MONTH)
+ " " + c.get(Calendar.HOUR_OF_DAY)
+ ":" + c.get(Calendar.MINUTE)
+ ":" + c.get((Calendar.SECOND));
但我得到的东西是这样的:
but I get something like this :
2011-8-1 12:05:20
这是这个问题的解决?对于一个月,我想我必须加1,对不对?
Which is the solution of this problem? For month I think I have to add 1,right?
推荐答案
您是过于复杂的问题。如果你需要的是格式化以某种方式的日期,然后用 java.text.SimpleDateFormat中
类。看到这里的文档:<一href=\"http://developer.android.com/reference/java/text/SimpleDateFormat.html\">http://developer.android.com/reference/java/text/SimpleDateFormat.html
You're over-complicating the problem. If all you need is to format a date in a certain way, then use java.text.SimpleDateFormat
class. See the documentation here: http://developer.android.com/reference/java/text/SimpleDateFormat.html
如果你确实需要得到各个部分,那么你在使用 Calendar.get
的方法是正确的。我建议你阅读了关于 java.util.Calendar中的
。
If you really do need to get individual parts, then you are correct in using Calendar.get
method. I suggest you read up on java.util.Calendar
.
这篇关于如何获得的月和日在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!