【问题标题】:How to save and retrieve Date in SharedPreferences如何在 SharedPreferences 中保存和检索日期
【发布时间】:2012-09-02 19:22:59
【问题描述】:

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

【问题讨论】:

    标签: java android date calendar sharedpreferences


    【解决方案1】:

    你可以这样做:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.US);
    

    保存日期:

    preferences .edit().putString("mydate", sdf.format(date)).apply();
    

    检索:

    try{
        Date date = sdf.parse(preferences.getString("myDate", "defaultValue"));
        } catch (ParseException e) {
                    e.printStackTrace();
                                   }
    

    希望对您有所帮助。

    【讨论】:

    • 硬编码字符串和语言环境,非常适合在发生任何变化时创建意外错误。
    【解决方案2】:

    tl;博士

    现代方法使用 java.time 类和 ISO 8601 字符串。

    阅读。

    Instant                             // Represent a moment in UTC with a resolution of nanoseconds.
    .ofEpochMilli( 
        Long.getLong( incomingText )  
    )                                   // Returns a `Instant` object.
    .atZone(                            // Adjust from UTC to some time zone. Same moment, same point on the timeline, different wall-clock time.
        ZoneId.of( "Europe/Paris" ) 
    )                                   // Returns a `ZonedDateTime` object.
    

    写作。

    ZonedDateTime
    .of(
        LocalDate.of( 2018 , Month.JANUARY , 23 ) ,
        LocalTime.of( 15 , 35 ) ,
        ZoneId.of( "Europe/Paris" ) 
    )                                   // Returns a `ZonedDateTime` object.
    .toInstant()                        // Returns an `Instant`. Adjust from a time zone to UTC. Same moment, same point on the timeline, different wall-clock time.
    .toEpochMilli()                     // Returns a `long` integer number primitive. Any microseconds or nanoseconds are ignored, of course.
    

    如果您的警报管理器尚未现代化以处理 java.time 对象,请使用添加到旧类的新方法在旧类和现代类之间进行转换。

    java.util.Date d = java.util.Date.from( instant ) ;
    

    ……和……

    Instant instant = d.toInstant() ;
    

    java.time

    麻烦的旧日期时间类被 java.time 类所取代。

    在 UTC 时间,分辨率为纳秒,使用 Instant

    Instant instant = Instant.now() ;  // Capture the current moment in UTC.
    

    您只需要毫秒来满足您的需求,因此请截断任何微秒和纳秒。

    Instant instant = Instant.now().truncatedTo( ChronoUnit.MILLIS ) ;
    

    要按日期和时间确定时刻,需要时区。时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

    如果没有指定时区,JVM 会隐式应用其当前的默认时区。该默认值可能在运行时(!)期间change at any moment,因此您的结果可能会有所不同。最好将您的 desired/expected time zone 明确指定为参数。

    continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

    ZoneId z = ZoneId.of( "America/Montreal" ) ;  
    LocalDate today = LocalDate.now( z ) ;
    

    如果你想使用 JVM 当前的默认时区,请求它并作为参数传递。如果省略,则隐式应用 JVM 的当前默认值。最好是明确的,因为默认值可能会在任何时候在运行时被 JVM 中任何应用程序的任何线程中的任何代码更改。

    ZoneId z = ZoneId.systemDefault() ;  // Get JVM’s current default time zone.
    

    或者指定一个日期。您可以通过数字设置月份,1 月至 12 月的编号为 1-12。

    LocalDate ld = LocalDate.of( 1986 , 2 , 23 ) ;  // Years use sane direct numbering (1986 means year 1986). Months use sane numbering, 1-12 for January-December.
    

    或者,更好的是,使用预定义的Month 枚举对象,一年中的每个月一个。提示:在整个代码库中使用这些 Month 对象,而不是仅仅使用整数,以使您的代码更具自记录性、确保有效值并提供 type-safety

    LocalDate ld = LocalDate.of( 1986 , Month.FEBRUARY , 23 ) ;
    

    结合时间,LocalTime

    LocalTime lt = LocalTime.of( 14 , 0 ) ;
    

    将它们全部包装成一个ZonedDateTime 对象。

    ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;
    

    通过提取 Instant 来调整到 UTC。

    Instant instant = zdt.toInstant() ;
    

    提取自 UTC 1970 年第一刻的纪元参考以来所需的毫秒数。同样,请注意,在提取毫秒时,Instant 中的任何微/纳米都将被忽略。

    long milliseconds = instant.toEpochMilli() ;  // Be aware of potential data loss, ignoring any microseconds or nanoseconds. 
    

    使用 Long 类从存储中读取这些毫秒作为文本。

    long milliseconds = Long.getLong( incomingText ) ;
    Instant instant = Instant.ofEpochMilli( milliseconds ) ;
    

    要通过特定地区(时区)的人们使用的挂钟时间的镜头看到那一刻,请申请ZoneId 以获取ZonedDateTime

    ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
    ZonedDateTime zdt = instant.atZone( z ) ;
    

    要生成表示该值的文本,请使用DateTimeFormatter.ofLocalizedDateTime 自动本地化。

    提示:考虑将您的日期时间值以标准ISO 8601 格式而不是毫秒计数写入存储。人类无法有意义地读取毫秒数,这使得调试和监控变得棘手。

    String output = instant.toString() ; 
    

    2018-10-05T20:28:48.584Z

    Instant instant = Instant.parse( 2018-10-05T20:28:48.584Z ) ;
    

    关于java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

    Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

    您可以直接与您的数据库交换 java.time 对象。使用符合JDBC 4.2 或更高版本的JDBC driver。不需要字符串,不需要java.sql.* 类。

    从哪里获得 java.time 类?

    ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可能会在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore

    【讨论】:

    • Java 8 版本不适用于旧版 Android,它从 API 26 开始
    • @Skynet 仔细阅读。寻找接近末尾的“Android”项目符号。
    • 对不起@BasilBourque,这是一个关于Java中日期时间的非常好的和完整的答案,但你根本不回答OP的问题。因此三年后很少有人支持。问题是“如何在 SharedPreferences 中保存和检索日期”,请在 TLDR 中添加一个适当的示例,从这样的日期开始:2021-03-22T00:03:00+00:00
    【解决方案3】:

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

    示例:

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

    您可以像这样使用它来保存或检索来自SharedPreferencesDate/Time 数据

    保存:

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

    回读:

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

    编辑

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

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

    【讨论】:

    • 看起来这种方式不适用于特定时区的日期。用例:为旅行用户存储日期
    猜你喜欢
    • 2018-06-03
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多