convert Date Format - Java java.util

Java examples for java.util:Date Format

Description

convert Date Format

Demo Code


//package com.java2s;

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

public class Main {
    final static SimpleDateFormat dateFormats[] = new SimpleDateFormat[] {
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US),
            new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z", Locale.US),
            new SimpleDateFormat("EEE, d MMM yy HH:mm z", Locale.US),
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US),
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm z", Locale.US),
            new SimpleDateFormat("EEE d MMM yy HH:mm:ss z", Locale.US),
            new SimpleDateFormat("EEE d MMM yy HH:mm z", Locale.US),
            new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss z", Locale.US),
            new SimpleDateFormat("EEE d MMM yyyy HH:mm z", Locale.US),
            new SimpleDateFormat("d MMM yy HH:mm z", Locale.US),
            new SimpleDateFormat("d MMM yy HH:mm:ss z", Locale.US),
            new SimpleDateFormat("d MMM yyyy HH:mm z", Locale.US),
            new SimpleDateFormat("d MMM yyyy HH:mm:ss z", Locale.US),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
                    Locale.getDefault()),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
                    Locale.getDefault()),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",
                    Locale.getDefault()),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX",
                    Locale.getDefault()),
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ",
                    Locale.getDefault()),
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()),
            new SimpleDateFormat("EEE, d MMM yy HH:mm:ss z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE, d MMM yy HH:mm z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE, d MMM yyyy HH:mm z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE d MMM yy HH:mm:ss z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE d MMM yy HH:mm z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE d MMM yyyy HH:mm:ss z",
                    Locale.getDefault()),
            new SimpleDateFormat("EEE d MMM yyyy HH:mm z",
                    Locale.getDefault()),
            new SimpleDateFormat("d MMM yy HH:mm z", Locale.getDefault()),
            new SimpleDateFormat("d MMM yy HH:mm:ss z", Locale.getDefault()),
            new SimpleDateFormat("d MMM yyyy HH:mm z", Locale.getDefault()),
            new SimpleDateFormat("d MMM yyyy HH:mm:ss z",
                    Locale.getDefault()), };

    public static Date convertDateFormat(String date) {
        for (SimpleDateFormat format : dateFormats) {
            format.setTimeZone(TimeZone.getTimeZone("UTC"));
            try {
                return format.parse(date);
            } catch (ParseException e) {
            }/*from   ww w. j a  v a 2  s  . c o m*/

            // try it again in english
            try {
                SimpleDateFormat enUSFormat = new SimpleDateFormat(
                        format.toPattern(), Locale.US);
                return enUSFormat.parse(date);
            } catch (ParseException e) {
            }
        }

        return null;
    }
}

Related Tutorials