Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

import java.util.TimeZone;

public class Main {
    private static SimpleDateFormat mIso8601Format;
    private static SimpleDateFormat mIso8601WithMillisFormat;

    /**
     * Get a date string in ISO 8601 format.
     *
     * @param date the Date object to convert to a String
     * @return a date string in ISO 8601 format.
     */
    public static String getIso8601StringfromDate(Date date) {
        initFormatter();
        return mIso8601WithMillisFormat.format(date);
    }

    private static void initFormatter() {
        if (mIso8601WithMillisFormat == null) {
            mIso8601WithMillisFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            mIso8601WithMillisFormat.setTimeZone(TimeZone.getTimeZone("UTC"));

            mIso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            mIso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
        }
    }
}