如何存储在共享preferences Date对象? [英] How to store a Date object in SharedPreferences?

查看:139
本文介绍了如何存储在共享preferences Date对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能存储日期对象使用共享preferences

在我的code其实我有一个字符串变量,布尔日期。这里是我的功能,用于存储所有的对象,除了日期。那么,如何可以做,请建议我?

 私人无效节省preferences(){

    字符串键=1;
    字符串值=你好;

    INT X = 5;

    日期的currentdate =新的日期();

    共享preferences共享preferences = GET preferences(MODE_APPEND);
    共享preferences.Editor编辑=共享preferences.edit();
    editor.putString(键,值);

    editor.putInt(2,5);

    editor.commit();
}
 

所以我的问题是如何存储日期使用共享preferences

解决方案

  editor.putLong(THE_DATE,currentDate.getTime());
 

你可以阅读日期从preferences是这样的:

 长米利斯=共享preferences.getLong(THE_DATE,0L);
日期theDate =新的日期(米利斯);
 

Is it possible to store a Date object using SharedPreferences?

Actually in my code I have a String variable, boolean and Date. Here is my function for storing all the objects except Date. So how that can be done please suggest me?

private void SavePreferences() {

    String key="1";
    String value="hello";

    int x=5;

    Date currentDate=new Date();

    SharedPreferences sharedPreferences = getPreferences(MODE_APPEND);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);

    editor.putInt("2",5);

    editor.commit();
}

So my question is how to store the Date using SharedPreferences?

解决方案

editor.putLong("THE_DATE", currentDate.getTime());

And you can read a Date from preferences like this:

long millis = sharedPreferences.getLong("THE_DATE", 0L);
Date theDate = new Date(millis);

这篇关于如何存储在共享preferences Date对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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