Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final SimpleDateFormat[] SUPPORTED_DATE_FORMATS = new SimpleDateFormat[] {
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm"),
            new SimpleDateFormat("yyyy-MM-dd'T'HH"), new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"),
            new SimpleDateFormat("yyyy-MM-dd"), new SimpleDateFormat("yyyy-MM"), new SimpleDateFormat("yyyy") };

    /**
     * Parses the given string into a Date using the supported date formats.
     * Returns null if the string cannot be parsed.
     *
     * @param dateString the date string.
     * @return a date.
     */
    public static Date parseDate(final String dateString) {
        if (dateString == null) {
            return null;
        }

        for (SimpleDateFormat format : SUPPORTED_DATE_FORMATS) {
            try {
                return format.parse(dateString);
            } catch (ParseException ignored) {
            }
        }

        return null;
    }
}