Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The simplest technique would be to use <code>DateFormat</code>:</p> <pre><code>String input = "Sat Feb 17 2012"; Date date = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH).parse(input); long milliseconds = date.getTime(); long millisecondsFromNow = milliseconds - (new Date()).getTime(); Toast.makeText(this, "Milliseconds to future date="+millisecondsFromNow, Toast.LENGTH_SHORT).show(); </code></pre> <p>A more difficult technique (that basically does what <code>DateFormat</code> does for you) involves parsing it yourself (this would <strong>not</strong> be considered <em>best practice</em>):</p> <pre><code>String input = "Sat Feb 17 2012"; String[] myDate = input.split("\\s+"); int year = Integer.parseInt(myDate[3]); String monthString = myDate[1]; int mo = monthString.equals("Jan")? Calendar.JANUARY : monthString.equals("Feb")? Calendar.FEBRUARY : monthString.equals("Mar")? Calendar.MARCH : monthString.equals("Apr")? Calendar.APRIL : monthString.equals("May")? Calendar.MAY : monthString.equals("Jun")? Calendar.JUNE : monthString.equals("Jul")? Calendar.JULY : monthString.equals("Aug")? Calendar.AUGUST : monthString.equals("Sep")? Calendar.SEPTEMBER : monthString.equals("Oct")? Calendar.OCTOBER : monthString.equals("Nov")? Calendar.NOVEMBER : monthString.equals("Dec")? Calendar.DECEMBER : 0; int day = Integer.parseInt(myDate[2]); Calendar c = Calendar.getInstance(); c.set(year, mo, day); long then = c.getTimeInMillis(); Time current_time = new Time(); current_time.setToNow(); long now = current_time.toMillis(false); long future = then - now; Date d = new Date(future); //TODO use d as you need. Toast.makeText(this, "Milliseconds to future date="+future, Toast.LENGTH_SHORT).show(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload