如何在SharedPreferences中保存和检索日期 [英] How to save and retrieve Date in SharedPreferences

查看:264
本文介绍了如何在SharedPreferences中保存和检索日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Android中的 SharedPreferences 中保存几个日期并检索它。我使用 AlarmManager 建立提醒应用程序,我需要保存未来日期的列表。它必须能够检索为毫秒。首先我想要计算今天时间和未来时间之间的时间,并存储在共享偏好。但是该方法不工作,因为我需要使用它 AlarmManager

解决方案

要保存并加载准确的日期,可以使用 long

  //以毫秒为单位获取当前时间,并从中创建一个Date对象:
Date date = new Date(System.currentTimeMillis ()); //或者简单的新Date();

//将其转换回毫秒表示形式:
long millis = date.getTime();

您可以使用它来保存或检索 Date / 时间来自 SharedPreferences 的数据



strong>保存:

  SharedPreferences prefs = ...; 
prefs.edit()。putLong(time,date.getTime())。apply();



回读

  Date myDate = new Date(prefs.getLong(time,0)); 

编辑



如果你想存储 TimeZone additionaly,你可以写一些帮助方法,这样的东西(我没有测试他们,随时纠正它,如果出错了):

  public static Date getDate(final SharedPreferences prefs,final String key,final Date defValue){
if(!prefs.contains(key +_value)||!prefs.contains(key +_zone)){
return defValue;
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(prefs.getLong(key +_value,0));
calendar.setTimeZone(TimeZone.getTimeZone(prefs.getString(key +_zone,TimeZone.getDefault()。getID())));
return calendar.getTime();
}

public static void putDate(final SharedPreferences prefs,final String key,final Date date,final TimeZone zone){
editor.edit()。putLong(key + _value,date.getTime())。apply();
editor.edit()。putString(key +_zone,zone.getID())。apply();
}


I need to save a few dates in SharedPreferences in android and retrieve it. I am building reminder app using AlarmManager and I need to save list of future dates. It must be able to retrieve as milliseconds. First I thought to calculate time between today now time and future time and store in shared preference. But that method is not working since I need to use it for AlarmManager.

解决方案

To save and load accurate date, you could use the long (number) representation of a Date object.

Example:

//getting the current time in milliseconds, and creating a Date object from it:
Date date = new Date(System.currentTimeMillis()); //or simply new Date();

//converting it back to a milliseconds representation:
long millis = date.getTime();

You can use this to save or retrieve Date/Time data from SharedPreferences like this

Save:

SharedPreferences prefs = ...;
prefs.edit().putLong("time", date.getTime()).apply();

Read it back:

Date myDate = new Date(prefs.getLong("time", 0));

Edit

If you want to store the TimeZone additionaly, you could write some helper method for that purpose, something like this (I have not tested them, feel free to correct it, if something is wrong):

public static Date getDate(final SharedPreferences prefs, final String key, final Date defValue) {
    if (!prefs.contains(key + "_value") || !prefs.contains(key + "_zone")) {
        return defValue;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(prefs.getLong(key + "_value", 0));
    calendar.setTimeZone(TimeZone.getTimeZone(prefs.getString(key + "_zone", TimeZone.getDefault().getID())));
    return calendar.getTime();
}

public static void putDate(final SharedPreferences prefs, final String key, final Date date, final TimeZone zone) {
    editor.edit().putLong(key + "_value", date.getTime()).apply();
    editor.edit().putString(key + "_zone", zone.getID()).apply();
}

这篇关于如何在SharedPreferences中保存和检索日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆