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.Calendar;

public class Main {
    /**
     * SimpleDateFormat with pattern yyyy-MM-dd
     */
    private static SimpleDateFormat formatYYYY_MM_DD;

    /**
     * get a "clean" calendar as 1900-01-01 00:00:00
     *
     * @return a "clean" calendar
     */
    public static Calendar getCleanCalendar() {
        long time = convertDateStr2Millis("1900-01-01");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        return calendar;
    }

    /**
     * convert date string with format yyyy-MM-dd to millisecond
     *
     * @param date
     * @return -1 if exception throw
     */
    public static long convertDateStr2Millis(String date) {
        if (date == null || date.trim().length() == 0)
            return -1;
        try {
            return formatYYYY_MM_DD.parse(date).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }
}