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 Date parseDate(String date, String datePartten) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(datePartten);
        Date d = df.parse(date);
        return d;
    }

    public static Date parseDate(String date) throws Exception {
        String[] datePartten = { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy/MM/dd HH:mm;ss", "yyyy/MM/dd",
                "yyyyMMddHHmmss", "yyyyMMdd" };
        return parseDate(date, datePartten);
    }

    public static Date parseDate(String date, String[] dateParttens) throws Exception {
        SimpleDateFormat df = null;
        Date d = null;
        boolean isParse = false;
        for (String partten : dateParttens) {
            df = new SimpleDateFormat(partten);
            try {
                d = df.parse(date);
                isParse = true;
                break;
            } catch (ParseException e) {
                isParse = false;
            }
        }
        if (!isParse) {
            throw new Exception();
        }
        return d;
    }
}