如何在 SharedPreferences 中保存和检索日期

How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和检索日期)
本文介绍了如何在 SharedPreferences 中保存和检索日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我需要在 android 的 SharedPreferences 中保存一些日期并检索它.我正在使用 AlarmManager 构建提醒应用程序,我需要保存未来日期列表.它必须能够以毫秒为单位进行检索.首先,我想计算现在时间和未来时间之间的时间并存储在共享偏好中.但该方法不起作用,因为我需要将它用于 AlarmManager.

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.

推荐答案

要保存和加载准确的日期,可以使用 Datelong(数字)表示对象.

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

示例:

//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();

您可以使用它从 SharedPreferences 中保存或检索 Date/Time 数据,如下所示

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

保存:

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

回读:

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

编辑

如果你想额外存储 TimeZone,你可以为此编写一些辅助方法,类似这样(我没有测试过它们,如果有问题,请随时更正):

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) {
    prefs.edit().putLong(key + "_value", date.getTime()).apply();
    prefs.edit().putString(key + "_zone", zone.getID()).apply();
}

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Month is not printed from a date - Java DateFormat(月份不是从日期打印的 - Java DateFormat)
java get week of year for given a date(java获取给定日期的一年中的一周)
Get the number of days, weeks, and months, since Epoch in Java(获取自 Java 中的 Epoch 以来的天数、周数和月数)
Is there a good way to get the date of the coming Wednesday?(有什么好方法可以得到即将到来的星期三的日期吗?)
Calendar view for Android GingerBread and before (APIlt;11)(Android GingerBread 及之前的日历视图 (API11))
Why is the month changed to 50 after I added 10 minutes?(为什么我加了 10 分钟后月份变成了 50?)