寻求一些帮助,从 DatePicker Widget 获取数据并将其存储在应用程序内的 sqlite 数据库中。我有以下代码:
java.util.Date utilDate = null;
String y = Integer.toString(date.getDayOfMonth()) + "/" + (date.getMonth()+1) + "/" + Integer.toString(date.getYear());
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyy");
utilDate = formatter.parse(y);
java.sql.Date z = new java.sql.Date(utilDate.getDate());
x = z.toString();
Log.v("The date: ", x);
}
其中日期是 DatePicker 小部件。如果我使用 logCat 输出 utilDate 变量(即日期的 Java 版本),它似乎工作正常,并给出如下格式: Tue Jan 04 00:00:00 GMT 2011 我期待它,但使用上面的代码来获取sql 版本的日期,它总是给我日期 1970-01-01。我很确定解决方案非常简单,但我就是看不到它。
最佳答案
这部分:utilDate.getDate()
是错误的。
引自Javadoc :
Returns the day of the month represented by this Date object. The value returned is between 1 and 31 representing the day of the month that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
因此,您会得到一个介于 1 到 31 之间的数字,并且您期望一个 long 表示自 1970 年 1 月 1 日 00:00:00.000 GMT 以来经过的毫秒数。
使用
java.sql.Date z = new java.sql.Date(utilDate.getTime());
一切都应该没问题。
关于java - Android 中的日期帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4593689/