title: How To Retrieve And Save a Date Via Shared Preferences
body: <h2>Original Post:</h2>
  
  <p>In the following example:</p>
  
  <p><a href="https://stackoverflow.com/questions/12343014/how-to-save-and-retrieve-date-in-sharedpreferences">How to save and retrieve Date in SharedPreferences</a></p>
  
  <p>What should I be looking to replace the ... with?</p>
  
  <pre><code>Save:
  SharedPreferences prefs = ...;
  SharedPreferences.Editor editor = prefs.edit();
  editor.putLong("time", date.getTime());
  editor.commit()
  </code></pre>
  
  <p>I'm attempting to use something such as:</p>
  
  <pre><code>SharedPreferences.Editor editor = getSharedPreferences(DATE_PREF, MODE_PRIVATE);
  </code></pre>
  
  <p>but I'm getting an error stating DATE_PREF cannot be resolved to a variable. </p>
  
  <pre><code>MY SOURCE:
  
  
          //get the current date
          Date date = new Date(System.currentTimeMillis());
  
          // convert the date to milliseconds
          long millis = date.getTime();
  
          // save the date to shared preferences
  
          SharedPreferences prefs = millis ;
          SharedPreferences.Editor editor = getSharedPreferences(DATE_PREF, MODE_PRIVATE);
          editor.putLong("time", date.getTime());
          editor.commit();
  </code></pre>
  
  <h2>Update After First Response:  (Help Still Needed)</h2>
  
  <p>After removing the lines: </p>
  
  <pre><code>// SharedPreferences prefs = millis;
      // SharedPreferences.Editor editor = PreferenceManager
      // .getDefaultSharedPreferences(getApplicationContext())
  </code></pre>
  
  <p>I'm getting two errors stating "editor cannot be resolved" at:</p>
  
  <pre><code>editor.putLong("time", date.getTime());
          editor.commit();
  </code></pre>
  
  <p>due to the fact I removed the lines which referenced the editor - my question is: will my preferences still be saved as shown below? (I need this value to persist after reboot.)</p>
  
  <p>I've tried using:</p>
  
  <p>SharedPreferences.putLong("time", date.getTime());
          SharedPreferences.commit();</p>
  
  <p>as well as:</p>
  
  <p>putLong("time", date.getTime());
          commit();</p>
  
  <p>However both methods are causing errors and I just want to make sure the values will be stored after reboot. </p>
  
  <pre><code>public class WifiMonitor extends Activity {
  
      Button sendButton;
  
      EditText msgTextField;
  
      private PendingIntent pendingIntent;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          TextView infoView = (TextView) findViewById(R.id.traffic_info);
  
          // get traffic info
          double totalBytes = (double) TrafficStats.getTotalRxBytes()
                  + TrafficStats.getTotalTxBytes();
          double mobileBytes = TrafficStats.getMobileRxBytes()
                  + TrafficStats.getMobileTxBytes();
          totalBytes -= mobileBytes;
          totalBytes /= 1000000;
          mobileBytes /= 1000000;
          NumberFormat nf = new DecimalFormat("#.##");
          String totalStr = nf.format(totalBytes);
          String mobileStr = nf.format(mobileBytes);
          String info = String.format(
                  "Wifi Data Usage: %s MB\tMobile Data Usage: %s MB", totalStr,
                  mobileStr);
          infoView.setText(info);
  
          // send traffic info via sms
          SmsManager smsManager = SmsManager.getDefault();
          smsManager.sendTextMessage("7862611848", null, info, null, null);
          String alarm = Context.ALARM_SERVICE;
  
          // get the current date
          Date date = new Date(System.currentTimeMillis());
  
          // convert the date to milliseconds
          long millis = date.getTime();
  
          // save the date to shared preferences
          SharedPreferences prefs = PreferenceManager
                  .getDefaultSharedPreferences(getApplicationContext());
          // SharedPreferences prefs = millis;
          // SharedPreferences.Editor editor = PreferenceManager
          // .getDefaultSharedPreferences(getApplicationContext());
  
          editor.putLong("time", date.getTime());
          editor.commit();
  
          // get the saved date
  
          Date myDate = new Date(prefs.getLong("time", 0));
      }
  
      // set the alarm to expire 30 days from the date stored in sharePreferences
      public void invokeAlarm(long invokeTime, long rowId) {
          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
          Intent i = new Intent(this, Alarm.class);
          i.putExtra("rowId", String.valueOf(rowId));
          am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
                  this, (int) System.currentTimeMillis(), i, 0));
      }
  
  }
  </code></pre>