Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, this question is already answered, but I'll drop my answer anyway. It might help someone.</p> <p>I've been looking for a <strong>solution for Android</strong> (API 7).</p> <ul> <li>Joda was out of the question - it is huge and suffers from slow initialization. It also seemed a major overkill for that particular purpose.</li> <li>Answers involving <code>javax.xml</code> won't work on Android API 7.</li> </ul> <p>Ended up implementing this simple class. It covers <strong>only the most common form</strong> of ISO 8601 strings, but this should be enough in some cases (when you're quite sure that the input will be in <em>this</em> format).</p> <pre><code>import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /** * Helper class for handling a most common subset of ISO 8601 strings * (in the following format: "2008-03-01T13:00:00+01:00"). It supports * parsing the "Z" timezone, but many other less-used features are * missing. */ public final class ISO8601 { /** Transform Calendar to ISO 8601 string. */ public static String fromCalendar(final Calendar calendar) { Date date = calendar.getTime(); String formatted = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ") .format(date); return formatted.substring(0, 22) + ":" + formatted.substring(22); } /** Get current date and time formatted as ISO 8601 string. */ public static String now() { return fromCalendar(GregorianCalendar.getInstance()); } /** Transform ISO 8601 string to Calendar. */ public static Calendar toCalendar(final String iso8601string) throws ParseException { Calendar calendar = GregorianCalendar.getInstance(); String s = iso8601string.replace("Z", "+00:00"); try { s = s.substring(0, 22) + s.substring(23); // to get rid of the ":" } catch (IndexOutOfBoundsException e) { throw new ParseException("Invalid length", 0); } Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse(s); calendar.setTime(date); return calendar; } } </code></pre> <p><strong>Performance note:</strong> I instantiate new SimpleDateFormat every time as means to avoid <a href="http://code.google.com/p/android/issues/detail?id=8258" rel="noreferrer">a bug</a> in Android 2.1. If you're as astonished as I was, see <a href="https://stackoverflow.com/questions/10624752/simpledateformat-timezone-bug-on-android">this riddle</a>. For other Java engines, you may cache the instance in a private static field (using ThreadLocal, to be thread safe).</p>
    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. 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