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.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static Date getDateFromString(String dateString) {

        DateFormat inputFormat = null;

        if (dateString == null) {
            return null;
        }

        if (dateString.length() == 19)
            inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
        if (dateString.length() == 10)
            inputFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

        if (inputFormat == null) {
            return null;
        }

        inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date parsed = null;
        try {
            parsed = inputFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return parsed;
    }
}