Parse string in ISO 8601 FORMAT "yyyy-MM-dd'T'HH:mm:ssz" to Date - Android java.util

Android examples for java.util:Date Parse

Description

Parse string in ISO 8601 FORMAT "yyyy-MM-dd'T'HH:mm:ssz" to Date

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ssz";

  /**//from www  .  ja va  2s.c  o m
   * Take an ISO 8601 string and return a Date object. On failure, returns null.
   */
  public static Date getDateFromIso8601String(String s) {
    SimpleDateFormat df = new SimpleDateFormat(ISO_8601_FORMAT);
    try {
      return df.parse(s);
    } catch (ParseException e) {
      return null;
    }
  }

}

Related Tutorials