如何在Android中获得月份和日子? [英] How to get month and day in android?
问题描述
我需要获得月份和日期:01为1月2日,02为2月,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));
但我得到这样的东西:
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
类。请参阅以下文档: 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屋!