久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

How to save and retrieve Date in SharedPreferences(如何在 SharedPreferences 中保存和檢索日期)
本文介紹了如何在 SharedPreferences 中保存和檢索日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我需要在 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 中保存和檢索日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 久久久激情 | 视频一二三区 | 欧美黑人一级爽快片淫片高清 | 亚洲免费一区二区 | 午夜精品一区二区三区在线观看 | 日韩精品视频在线免费观看 | 精区3d动漫一品二品精区 | 国产在线小视频 | 观看av | 四虎最新视频 | 国产精品中文在线 | 国产福利在线播放麻豆 | 欧美综合久久 | 欧美电影一区 | 成年免费大片黄在线观看岛国 | 亚洲精品免费视频 | 欧美在线资源 | 亚洲精品中文字幕在线观看 | www.国产精 | 国产精品成人国产乱一区 | ririsao久久精品一区 | 又黄又爽的网站 | 狠狠干天天干 | 久久亚 | 日韩精品成人免费观看视频 | 国产一区日韩在线 | 欧美精品三区 | 一区二区三区四区在线播放 | 精品久久久久久久久久久久 | 高清国产一区二区 | 国产欧美一区二区精品忘忧草 | 国产一区二区三区四区 | 欧美日韩高清在线观看 | 黄色一级大片在线免费看产 | 91色视频在线观看 | 亚洲免费一区二区 | 免费大黄视频 | 亚洲精品在线免费观看视频 | 国产精品一区二区在线播放 | 国产成人啪免费观看软件 | 精品视频在线免费观看 |