Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Date Conversion
    primarykey
    data
    text
    <p>I'm receiving some date format, an ISO-something, and was wondering what the appropriate way to parse it into a timestamp.</p> <p>My ultimate goal will be a standard switch on it to create "X minutes / hours / days ago.", so if there is a tool that does that already, that'd be optimal.</p> <p>I've tried joda-time, but it choked on it.</p> <p>Here is an example of a time string:</p> <p><code>2012-05-03 @ 15.55.05.433Z</code></p> <p>Additionally, if it is indeed it is the case that it's not an ISO-something format, I've no problem manipulating, say, the <code>@</code> out of it, etc.</p> <p><strong>EDIT:</strong> Attempt 1:</p> <pre><code>SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSSZ" ); try { Date myDate = dateFormat.parse( o.getCreationDate().replace( "@", "" ) ); long minutes = myDate.getTime() / 1000 / 60; holder.timeText.setText( minutes + " minutes ago" ); } catch( ParseException e ) { e.printStackTrace(); } </code></pre> <p>Result:</p> <p><code>05-17 16:58:05.003: W/System.err(2915): java.text.ParseException: Unparseable date: "2012-05-09 19:49:47.987Z" (at offset 24) </code></p> <p><strong>EDIT 2:</strong> Stripping the Z, and correct millisecond to minute conversion fixed it:</p> <pre><code>SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" ); try { Date myDate = dateFormat.parse( o.getCreationDate().replace( "@ ", "" ).replace( "Z", "" ) ); Date now = new Date(); long minutes = ( now.getTime() - myDate.getTime() ) / 1000 / 60; holder.timeText.setText( minutes + " minutes ago" ); } catch( ParseException e ) { e.printStackTrace(); } </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.
 

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