0

你好,我的兄弟姐妹我是 android 新手,目前我在 android 数据库站点工作。我的应用程序用户使用了来自多个国家/地区的应用程序,所以我的问题是当用户执行任何数据库事务时,我如何插入来自不同区域设置的每个用户的当前时间,即使用户的 android 移动设备当前时间设置不正确但我想在他所在地区的数据库中插入当前时间。

注意:目前我正在使用 MySQL 数据库

现在我以这种方式插入用户 i 的当前时间

String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).concat(" 00:00:00");

但这仅适用于设置默认时区的一种语言环境。所以,请有人帮我解决我的问题。

4

2 回答 2

0

我建议将 SQL 中的 DateTime 类型保持为 Long。请尝试以下代码:

Long now = System.currentTimeMillis();

如果需要,您可以将 Long 值转换为当前语言环境的字符串:

DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Session.currentLocale);
String date = dateFormat.format(yourStoredDateAsLong);

或者

String date = dateFormat.format(System.currentTimeMillis());
于 2015-02-13T07:55:40.560 回答
0

为了在数据库中节省时间,最好和最兼容国家/地区的方法是使用时间作为 Long。例如,创建一个日历实例:

   Calendar cal = Calendar.getInstance();
   Long timeToSave = cal.getTimeInMillis();

然后您必须将此 Long 值保存在数据库中。使用此值,您可以设置所有时区的所有日期格式,也可以设置 AM/PM 值等。

于 2015-02-13T07:47:48.387 回答