为什么这段代码返回 0001-02-05?
public static String getNowDate() throws ParseException
{
return Myformat(toFormattedDateString(Calendar.getInstance()));
}
我将代码更改为:
public static String getNowDate() throws ParseException
{
Calendar temp=Calendar.getInstance();
return temp.YEAR+"-"+temp.MONTH+"-"+temp.DAY_OF_MONTH;
}
现在它返回 1-2-5。
请帮我确定实际日期。我只需要 Sdk 日期。
最佳答案
Calendar.YEAR
、Calendar.MONTH
、Calendar.DAY_OF_MONTH
是 int
常量(只需在API doc )...
因此,正如@Alex 发布的那样,要从 Calendar
实例中创建格式化的 String
,您应该使用 SimpleDateFormat。
如果您需要特定字段的数字表示,请使用 get(int)
函数:
int year = temp.get(Calendar.YEAR);
int month = temp.get(Calendar.MONTH);
int dayOfMonth = temp.get(Calendar.DAY_OF_MONTH);
警告!月份从 0 开始!!!我因此犯了一些错误!
关于java - 日历返回错误的当前日期android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13074588/