Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* 
 * Copyright (C) 2012-2014 Open Source Consulting, Inc. All rights reserved by Open Source Consulting, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2014. 8. 20.      First Draft.
 */

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

import java.util.Date;

public class Main {
    public static String DEFAULT_DATE_PATTERN = "yyyy.MM.dd HH:mm:ss";

    private static Date getDate(String date) throws ParseException {
        if (date == null) {
            return null;
        }

        int yyyy = Integer.parseInt(date.substring(0, 4));
        int mm = Integer.parseInt(date.substring(4, 6));
        int dd = Integer.parseInt(date.substring(6, 8));
        int hh = Integer.parseInt(date.substring(8, 10));
        int mi = Integer.parseInt(date.substring(10, 12));
        int ss = Integer.parseInt(date.substring(12, 14));

        if (yyyy <= 1900 || yyyy >= 2999) {
            throw new ParseException("Invalid year.", yyyy);
        }
        if (mm < 1 || mm > 12) {
            throw new ParseException("Invalid month.", mm);
        }
        if (dd < 1 || dd > 31) {
            throw new ParseException("Invalid Day.", dd);
        }
        if (hh < 0 || hh > 23) {
            throw new ParseException("Invalid hour.", hh);
        }
        if (mi < 0 || mi > 59) {
            throw new ParseException("Invalid minute.", mi);
        }
        if (ss < 0 || ss > 59) {
            throw new ParseException("Invalid second.", ss);
        }

        return new SimpleDateFormat(DEFAULT_DATE_PATTERN).parse(date);
    }
}