java - 日历返回错误的当前日期android

标签 java android calendar

为什么这段代码返回 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.YEARCalendar.MONTHCalendar.DAY_OF_MONTHint 常量(只需在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/

相关文章:

asp.net - 如何在asp-Repeater项目模板中显示模式?

Python dateutil.rrule 非常慢

java - Google 自定义搜索 API : how to get search result contents description (e. g 片段)用于 URL

android - 为什么我在 google playstore 上的支持设备列表是空的

java - material-calendarview 设置日期的背景色

android - Google Play 服务已过时。需要 4030500 但找到 3266132

android - 如何通过改造发送 List<String> ?

java - 为什么我的 Lib-GDX 项目不是 GWT 项目?

基于 Unix 时间戳的 Java 范围聚合

java - NoSuchMethodError 在我的 android 设备上运行时确实存在的方法