Format a Date into ISO 8601 Complaint format. - Android java.util

Android examples for java.util:Date Format

Description

Format a Date into ISO 8601 Complaint format.

Demo Code

/**/*from w ww .j a  v a  2s.  c o  m*/
 * Copyright (c) 2012 Todoroo Inc
 *
 * See the file "LICENSE" for the full license governing this code.
 */
//package com.java2s;

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";

    /**
     * Format a Date into ISO 8601 Complaint format.
     * @return date string, or empty string if input was null
     */
    public static String getIso8601String(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat(ISO_8601_FORMAT);
        String result = "";
        if (d != null) {
            result = sdf.format(d);
        }
        return result;
    }
}

Related Tutorials