Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

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

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

public class Main {
    /**
     * Year-Month-Day pattern: 2015-01-28
     */
    public static final String DATE_YEAR_MONTH_DAY_STRING_FORMAT = "yyyy-MM-dd";

    /**
     * Standard format 'yyyy-MM-dd'
     *
     * @param dateString The string to parse
     * @return The date or null
     */
    public static Date stringToDate(String dateString) {
        return stringToDate(dateString, DATE_YEAR_MONTH_DAY_STRING_FORMAT);
    }

    /**
     * Converts a string representation of a date to that date
     *
     * @param dateString The string to parse
     * @param format     The format to parse the string with
     * @return The date, or null
     */
    public static Date stringToDate(String dateString, String format) {
        if (dateString == null) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(format, Locale.US);
        try {
            return df.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}