get a "clean" calendar as 1900-01-01 00:00:00 - Java java.util

Java examples for java.util:Calendar Calculation

Description

get a "clean" calendar as 1900-01-01 00:00:00

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {
    /**/*from   w w w. j a v a2s  . c o  m*/
     * 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;
    }
}

Related Tutorials