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.Calendar;
import java.util.Date;
import java.util.Locale;

public class Main {
    public static final String DEFAUL_PARSE = "yyyy-MM-dd HH:mm:ss";

    public static Date getDate(String dateStr) throws ParseException {
        return getDate(dateStr, DEFAUL_PARSE);
    }

    public static Date getDate(String dateStr, String parsePattern) throws ParseException {
        Locale locale = Locale.CHINA;
        SimpleDateFormat dataformat = new SimpleDateFormat(parsePattern, locale);
        Date date = dataformat.parse(dateStr);
        return date;
    }

    public static Date getDate(int year, int month, int day, int hour, int min, int sec) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, (month - 1), day, hour, min, sec);
        return calendar.getTime();
    }
}