com.qdum.llhb.common.utils.DateUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.qdum.llhb.common.utils.DateUtils.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.qdum.llhb.common.utils;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;
import com.jfinal.plugin.activerecord.Record;

/**
 * , org.apache.commons.lang.time.DateUtils
 *
 * @author ThinkGem
 * @version 2013-3-15
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {

    private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
            "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" };

    /**
     * ? ?yyyy-MM-dd
     */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * ? ?yyyy-MM-dd pattern?"yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     *  ?yyyy-MM-dd pattern?"yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        String formatDate = null;
        if (pattern != null && pattern.length > 0) {
            formatDate = DateFormatUtils.format(date, pattern[0].toString());
        } else {
            formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
        }
        return formatDate;
    }

    /**
     * ? ?HH:mm:ss
     */
    public static String getTime() {
        return formatDate(new Date(), "HH:mm:ss");
    }

    /**
     * ? ?yyyy-MM-dd HH:mm:ss
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * ? ?yyyy
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * ? ?MM
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     *  ?dd
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * ? ?E
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    /**
     *  ?
     * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
     * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm" }
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * ?
     *
     * @param date
     * @return
     */
    public static long pastDays(Date date) {
        long t = new Date().getTime() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    /**
     * 
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long pastMinutes(Date date1, Date date2) {
        long t = date1.getTime() - date2.getTime();
        return t / (60 * 1000);
    }

    /**
     * @param args
     * @throws ParseException
     */
    public static void main(String[] args) throws ParseException {
        //      System.out.println(formatDate(parseDate("2010/3/6")));
        //      System.out.println(getDate("yyyyMMdd E"));
        //      long time = new Date().getTime()-parseDate("2012-11-19").getTime();
        //      System.out.println(time/(24*60*60*1000));
    }

    /**
     * ?
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    /**
     * getMonthFirstDay??:??
     *
     * @author yaoyt
     * @time 15/12/29 ?1:19
     */
    public static Date getMonthFirstDay() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//1?,?
        return c.getTime();
    }

    /**
     * getMonthLastDay??:???
     *
     * @author yaoyt
     * @time 15/12/29 ?1:21
     */
    public static Date getMonthLastDay() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * ??
     *
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean isSameWeek(Date date1, Date date2) {
        Calendar c = Calendar.getInstance();
        c.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        int week1 = c.get(Calendar.WEEK_OF_YEAR);
        int week2 = c2.get(Calendar.WEEK_OF_YEAR);
        if (week1 == week2) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    /**
     * ??
     *
     * @return
     */
    public static Boolean isSameMonth(Date date1, Date date2) {
        Calendar c = Calendar.getInstance();
        c.setTime(date1);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(date2);
        int month = c.get(Calendar.MONTH);
        int month2 = c2.get(Calendar.MONTH);
        if (month == month2) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }

    /**
     * ?
     * @author zhaoyl
     * @param Datedate-- ?
     * @time 20161610:05:06
     * */
    public String setPastTime(Date date) {
        long pastTime = new Date().getTime() - date.getTime();//
        if (pastTime < 24 * 3600 * 1000) {//?
            if (pastTime < 3600 * 1000) {
                if (pastTime < 60 * 1000) {
                    return "";
                } else {
                    return (pastTime / (60 * 1000)) + "?";
                }
            } else {
                return (pastTime / (3600 * 1000)) + "??";
            }
        } else {
            return (pastTime / (24 * 3600 * 1000)) + "?";
        }
    }

    /**
      * ?
      *
      * @param d
      * @param day
      * @return
      */
    public static String getDateAfter(int day) {
        Calendar now = Calendar.getInstance();
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return formatDate(now.getTime(), "yyyy-MM-dd HH:mm:ss");
    }
}