-2
I asked same question before but it got closed and associated with another question which not solving my problem.

I want an app to do particular thing once in a day, when user opens the app first time in day. To do so I want to store a date in SharedPreference. But it only stores int, boolean, string etc.

/*

I'm showing my logic here. If there is better way plz tell me!!!
I will give some by default value like 00/00/0000 for initialization.
The first time user starts the app then I will do that particular work and initialize sharedPreference by current date 16/03/2020. 
If user again opens app by same date I don't do that work(By checking curdate and sharedPreference date).
If user opens app on another date I check currentDate and sharedPreference date, if they are different I will do that work and edit the sharedPreference by that day.
*/

So tell me how can store date in sharedPreference?

  • what kind of data you want to store – Fahad Alotaibi Mar 16 '20 at 09:11
  • You need to convert `Date` to `String` and store into `SharedPreference`. If you want to get that date then get date from `SharedPreference` and convert `String` TO `Date` – Ali Mar 16 '20 at 09:12
  • you can convert the object to json string then store it – Fahad Alotaibi Mar 16 '20 at 09:13
  • just convert data into string and store into sharedPreference and when you get it agagin convert it into date simple – Amit pandey Mar 16 '20 at 09:13
  • Yes as Ali and Amit said you have convert the Date object to String and save it. – Sanjeev Mar 16 '20 at 09:15
  • I have shown my problem in comments. So even I'm not sure what is better way to store so plz tell me – Aayush dual Mar 16 '20 at 09:25
  • @Aayushdual StackOverflow is not a code-writing service. Please read through the [Help Center](https://stackoverflow.com/help), in particular, [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include [Minimum, Complete, Verifiable Example](https://stackoverflow.com/help/minimal-reproducible-example). People will be glad to help – Ali Mar 16 '20 at 09:29
  • Please think about how you approach people as your attitude could be misunderstood as "we should tell you how to solve this" ;-) If you ask in a polite way and document what you have tried then people are more willing to help instead of giving a negative point for your question. You are new here so just giving you some hints :-) – Beauvais Mar 16 '20 at 11:30

2 Answers2

1

You can save date in SharedPreferences by convert Date to String. Kindly find the example of saving date by method "savedates". you will pass the context and date in string. context would be current context.

    public static void savedates(Context context, String date) {
    SharedPreferences.Editor editor = context.getSharedPreferences("MyPref", MODE_PRIVATE).edit();
    editor.putString("datestr", date);
    editor.apply();
}

    public static String getDates(Context context) {
    SharedPreferences prefs = context.getSharedPreferences("MyPref", MODE_PRIVATE);
     return prefs.getString("datestr", "");
}
Satish
  • 527
  • 6
  • 13
1
//create sharedpreference object
 SharedPreferences.Editor myeditor = context.getSharedPreferences("MyPref", MODE_PRIVATE).edit();

// to get current date.
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
String date = sdf.format(new Date());

//store date in sharedpreference
myeditor.putString("mydate",date);
myeditor.commit();
  • I combined both answers. So thx to you too!!! – Aayush dual Mar 16 '20 at 10:22
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Mar 17 '20 at 12:33