为什么此代码返回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的日期。
发布于 2012-10-26 02:10:25
使用SimpleDateFormat
new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());您正在使用要与Calendar.get()方法一起使用的常量。
发布于 2012-10-26 02:10:27
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开始!因为这个我犯了一些错误!
发布于 2012-10-26 02:12:11
为什么不使用SimpleDateFormat
public static String getNowDate() {
return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}https://stackoverflow.com/questions/13074588
复制相似问题