cn.mypandora.util.MyDateUtils.java Source code

Java tutorial

Introduction

Here is the source code for cn.mypandora.util.MyDateUtils.java

Source

/**
 * Copyright  2015.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.mypandora.util;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.util.*;

/**
 * ?
 * <p>User: kaibo
 * <p>Date: 2015/7/17
 * <p>Version: 1.0
 */
public class MyDateUtils {
    public final static String DATE_FORMAT = "yyyy-MM-dd";
    public final static String DATE_FORMAT_CN = "yyyyMMdd";
    public final static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String TIME_FORMAT_CN = "yyyyMMdd HH:mm:ss";
    public final static String MONTH_FORMAT = "yyyy-MM";
    public final static String DAY_FORMAT = "yyyyMMdd";

    /**
     * ?.
     *
     * @return
     */
    public static String getMonthFirstDay() {
        Calendar cal = Calendar.getInstance();
        // ,?.
        // Calendar f = (Calendar) cal.clone();
        // f.clear();
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        // f.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DATE));
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // .
        cal.set(Calendar.DATE, 1);
        return DateFormatUtils.format(cal, DATE_FORMAT);

    }

    /**
     * ??
     *
     * @return
     */
    public static String getMonthLastDay() {
        Calendar cal = Calendar.getInstance();
        Calendar f = (Calendar) cal.clone();
        f.clear();
        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        // f.set(Calendar.MILLISECOND, -1);
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        // f.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // (?)
        cal.set(Calendar.DATE, 1);// ?1?
        cal.add(Calendar.MONTH, 1);// ?1?
        cal.add(Calendar.DATE, -1);// ???
        return DateFormatUtils.format(cal, DATE_FORMAT);
    }

    /**
     * 
     *
     * @return
     */
    public static String getPreviousMonthFirst() {
        Calendar cal = Calendar.getInstance();
        Calendar f = (Calendar) cal.clone();
        f.clear();
        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        // f.set(Calendar.DATE, 1);
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        // f.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DATE));
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // (?)
        cal.set(Calendar.DATE, 1);// ?1?
        cal.add(Calendar.MONTH, -1);
        return DateFormatUtils.format(cal, DATE_FORMAT);
    }

    /**
     * ?
     *
     * @return
     */
    public static String getPreviousMonthEnd() {
        Calendar cal = Calendar.getInstance();
        Calendar f = (Calendar) cal.clone();
        f.clear();
        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        // f.set(Calendar.MILLISECOND, -1);
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        // f.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // (?)
        cal.set(Calendar.DATE, 1);// ?1?
        cal.add(Calendar.MONTH, 0);//
        cal.add(Calendar.DATE, -1);// ???
        return DateFormatUtils.format(cal, DATE_FORMAT);
    }

    /**
     * 
     *
     * @return
     */
    public static String getNextMonthFirst() {
        Calendar cal = Calendar.getInstance();
        Calendar f = (Calendar) cal.clone();
        f.clear();
        // 
        // f.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        // f.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
        // f.set(Calendar.DATE, 1);
        // or f.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DATE));
        // return DateFormatUtils.format(f, DATE_FORMAT);

        // 
        cal.set(Calendar.DATE, 1);// ?1?
        cal.add(Calendar.MONTH, +1);// ?1?
        return DateFormatUtils.format(cal, DATE_FORMAT);
    }

    /**
     * ?
     *
     * @return
     */
    public static String getNextMonthEnd() {
        Calendar cal = Calendar.getInstance();
        // cal.set(Calendar.DATE, +1);
        // cal.add(Calendar.MONTH, +2);
        // cal.add(Calendar.DATE, -1);
        // return DateFormatUtils.format(cal, DATE_FORMAT);

        // 
        cal.add(Calendar.MONTH, 1);// 
        cal.set(Calendar.DATE, 1);// 
        cal.roll(Calendar.DATE, -1);// ?
        return DateFormatUtils.format(cal, DATE_FORMAT);
    }

    /**
     * ?
     *
     * @return
     */
    public static int getCurrentMonthDays() {
        Calendar cal = new GregorianCalendar();// Calendar.getInstance();
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return days;
    }

    /**
     * 
     *
     * @param date  yyyy-MM
     * @return
     */
    public static int getSpecifiedMonthDays(String date) {
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(DateUtils.parseDate(date, MONTH_FORMAT));
            int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            return days;
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return 0;
    }

    /**
     * ?
     *
     * @return
     */
    public static String getCurrentMonth() {
        Calendar cal = Calendar.getInstance();
        String currentMonth = DateFormatUtils.format(cal, MONTH_FORMAT);
        return currentMonth;
    }

    /**
     * ?
     *
     * @return
     */
    public static String getCurrentDate() {
        Calendar cal = Calendar.getInstance();
        String currentDate = DateFormatUtils.format(cal, DATE_FORMAT);
        return currentDate;
    }

    /**
     * ?
     *
     * @return
     */
    public static String getCurrentTime() {
        Calendar cal = Calendar.getInstance();
        String currentDate = DateFormatUtils.format(cal, TIME_FORMAT);
        return currentDate;
    }

    /**
     * ??
     *
     * @return
     */
    public static String getPreviousMonthDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); // ?
        calendar.add(Calendar.MONTH, -1); // ?

        String currentDate = DateFormatUtils.format(calendar, DATE_FORMAT);
        return currentDate;
    }

    /**
     * ????X
     *
     * @param offset
     * @return
     */
    public static String getOffsetDate(int offset) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, offset);
        String currentDate = DateFormatUtils.format(cal, DATE_FORMAT);
        return currentDate;
    }

    /**
     * ???X
     *
     * @param specifiedDate ,?YYYY-MM-DD
     * @param offset        ??
     * @return
     * @throws ParseException
     */
    public static String getSpecifiedOffsetDate(String specifiedDate, int offset) throws ParseException {
        Date date = DateUtils.parseDate(specifiedDate, DATE_FORMAT);
        Calendar cal = DateUtils.toCalendar(date);
        cal.add(Calendar.DAY_OF_MONTH, offset);
        String returnDate = DateFormatUtils.format(cal, DATE_FORMAT);
        return returnDate;
    }

    /**
     * ???X
     *
     * @param specifiedTime ,?yyyy-MM-dd HH:mm:ss
     * @param offset        ??
     * @return
     * @throws ParseException
     */
    public static String getSpecifiedOffsetTime(String specifiedTime, int offset) throws ParseException {
        Date date = DateUtils.parseDate(specifiedTime, TIME_FORMAT);
        Calendar cal = DateUtils.toCalendar(date);
        cal.add(Calendar.DAY_OF_MONTH, offset);
        String returnDate = DateFormatUtils.format(cal, TIME_FORMAT);
        return returnDate;
    }

    /**
     * ???X
     *
     * @param specifiedDateTime ,?yyyy-MM-dd HH:mm:ss/yyyy-MM-dd
     * @param offset            ??
     * @return
     * @throws ParseException
     */
    public static String getOffsetDateTime(String specifiedDateTime, int offset) throws ParseException {
        String regexStr = "\\d{4}-\\d{2}-\\d{2}";
        if (specifiedDateTime.matches(regexStr)) {
            return getSpecifiedOffsetDate(specifiedDateTime, offset);
        } else {
            return getSpecifiedOffsetTime(specifiedDateTime, offset);
        }
    }

    /**
     * ?
     *
     * @param year
     * @return
     */
    public static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }

    /**
     * .
     *
     * @param c
     * @return
     */
    public static String getWeekDay(Calendar c) {
        if (c == null) {
            return "";
        }
        switch (c.get(Calendar.DAY_OF_WEEK)) {
        case Calendar.MONDAY:
            return "";
        case Calendar.TUESDAY:
            return "";
        case Calendar.WEDNESDAY:
            return "";
        case Calendar.THURSDAY:
            return "";
        case Calendar.FRIDAY:
            return "";
        case Calendar.SATURDAY:
            return "";
        default:
            return "";
        }
    }

    /**
     * .
     *
     * @param begin  .
     * @param end   ? .
     * @return
     */
    public static List<String> getDaysListBetweenDates(String begin, String end) {
        List<String> dateList = new ArrayList<String>();
        Date d1;
        Date d2;
        try {
            d1 = DateUtils.parseDate(begin, DATE_FORMAT);
            d2 = DateUtils.parseDate(end, DATE_FORMAT);
            if (d1.compareTo(d2) > 0) {
                return dateList;
            }
            do {
                dateList.add(DateFormatUtils.format(d1, DATE_FORMAT));
                d1 = DateUtils.addDays(d1, 1);
            } while (d1.compareTo(d2) <= 0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

    /**
     * 
     *
     * @param begin
     * @param end
     * @return
     */
    public static List<String> getMonthsListBetweenDates(String begin, String end) {
        List<String> dateList = new ArrayList<String>();
        Date d1;
        Date d2;
        try {
            d1 = DateUtils.parseDate(begin, DATE_FORMAT);
            d2 = DateUtils.parseDate(end, DATE_FORMAT);
            if (d1.compareTo(d2) > 0) {
                return dateList;
            }
            do {
                dateList.add(DateFormatUtils.format(d1, MONTH_FORMAT));
                d1 = DateUtils.addMonths(d1, 1);
            } while (d1.compareTo(d2) <= 0);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

    /**
     * long???yyyy-MM-dd HH:mm:ss
     *
     * @param createTime
     * @return
     */
    public static String timestamp2Time(String createTime) {
        long fooTime = Long.parseLong(createTime) * 1000L;
        return DateFormatUtils.format(fooTime, TIME_FORMAT);
    }

    /**
     * ?yyyy-MM-dd?
     *
     * @param date
     * @return
     */
    public static String date2StrDate(Date date) {
        return DateFormatUtils.format(date, DATE_FORMAT);
    }

    /**
     * ?yyyy-MM-dd HH:mm:ss?
     *
     * @param date
     * @return
     */
    public static String date2StrTime(Date date) {
        return DateFormatUtils.format(date, TIME_FORMAT);
    }

    public static void main(String[] args) throws ParseException {
        //        System.out.println(getMonthFirstDay());
        //        System.out.println(getMonthLastDay());
        //        System.out.println(getPreviousMonthFirst());
        //        System.out.println(getPreviousMonthEnd());
        //        System.out.println(getNextMonthFirst());
        //        System.out.println(getNextMonthEnd());
        //        System.out.println(getCurrentMonthDays());
        //        System.out.println(getSpecifiedMonthDays("1900-02"));
        //        System.out.println(getCurrentMonth());
        //        System.out.println(getCurrentDate());
        //        System.out.println(getOffsetDate(-4));
        //        System.out.println(isLeapYear(1900));
        //        System.out.println(getWeekDay(Calendar.getInstance()));
        //        System.out.println(getDaysListBetweenDates("2012-1-12", "2012-1-21"));
        //        System.out.println(getMonthsListBetweenDates("2012-1-12", "2012-3-21"));
        //        System.out.println(getSpecifiedOffsetTime("2012-09-09 12:12:12", 12));
        //        System.out.println(getOffsetDateTime("2012-09-09", 12));
        //        System.out.println(getOffsetDateTime("2012-09-09 12:12:12", 12));
        //        System.out.println(long2Time("1234567890"));
        System.out.println(date2StrDate(new Date()));
        System.out.println(date2StrTime(new Date()));

    }
}