-5

I am calling an API and as per the guidelines calling it every time is inefficient as the data does not change that regularly. They recommend calling it once and then not polling until 10 minutes has passed.

I am calling this from an Android app and so I want to store the current date plus 10 minutes. I do this like so:

Date forecastRefreshDate = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(forecastRefreshDate);
cal.add(Calendar.MINUTE, 10);
editor.putString("ForecastRefreshDate", forecastRefreshDate.toString());
editor.apply();

So now when this code is run again it needs to check if the current time (new Date()) is > the value saved in the cache/app file.

How do I create a Date variable equal to the value stored in the cache as a String?

andrewb
  • 2,995
  • 7
  • 54
  • 95
  • Likely because this question could be easily answered with some research. You could find the format of the String `Date.toString()` returns and look into parsing the result. – CubeJockey May 07 '15 at 15:29

4 Answers4

1

Convert date to epoch time, which is a number of milliseconds stored as a long. Store long as string and parse back into long when needed.

long epoch = date.getTime(); //where date is your Date
String yourString = Long.toString(epoch); //to string for storage
long time = Long.valueOf(yourString).longValue(); ; //back to epoch
Date originaldate = new Date(Long.parseLong(time)); //back to date
Eric S.
  • 1,502
  • 13
  • 31
1

When dealing with time, which is needed only internally to measure time periods, It is much better to store timestamps as long value (millis sicne 1.1.1970 UTC).

Use long timestamp = System.currentTimeMillis()

If a String storage is neccesary simply convert the Long to a String.

String timeStampStr = String.parseLong(timeStamp);

Do not use Date.toString for other things than for logging or debugging. The toString() representation may be different in other countries. If a human readable timestamp string is needed you have to specify a Specific DateFormat, see also DateFormatter.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
1

Parsing a long it the most efficent way, however there may be cases that you have already formatted a date to some other format and want to parse that. Then you should use this

private static final String format = "yyyy-MM-dd HH:mm:ss";
private static final SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.ENGLISH);
private Calendar dateTime = Calendar.getInstance();

public void setDateTime(String dateTimeString) {
    try {
        dateTime.setTime(formatter.parse(dateTimeString));
    } catch (ParseException e) {
         // Dummy handled exception
        dateTime.setTimeInMillis(0);
    }
}

Parse a long containing milliseconds since epoch

Date date = new Date(Long.parseLong(timeInMillisecondsString));
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
0

Since you're using a plain toString() it should be coverable using a SimpleDateFormat.

Try this:

DateFormat dateFormat = new SimpleDateFormat();
Date myDate = dateFormat.parse(myDateString);
Dave Lugg
  • 2,326
  • 2
  • 15
  • 23