Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: MIT License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;

public class Main {
    private static SimpleDateFormat yyyyMMddFormat;

    /**
     * Get the Date from String with custom format. Default format is yyyy-MM-dd
     *
     * @param dateString
     * @param dateFormat
     * @return
     * @throws ParseException
     */
    public static Date getDateFromString(String dateString, String dateFormat, Locale locale)
            throws ParseException {
        SimpleDateFormat formatter;

        if (dateFormat == null) {
            if (yyyyMMddFormat == null) {
                setup(locale);
            }

            formatter = yyyyMMddFormat;
        } else {
            formatter = new SimpleDateFormat(dateFormat, Locale.ENGLISH);
        }

        return formatter.parse(dateString);
    }

    public static void setup(Locale locale) {
        yyyyMMddFormat = new SimpleDateFormat("yyyy-MM-dd", locale);
    }
}