vo中对date格式的时间格式的处理,方便到前台展示

之前一个同事的写法是这样的(绿色的):

 


public static final String GENERAL_PATTERN_2 = "yyyy-MM-dd HH:mm:ss";

 其实没必要这样,在vo里面的set方法做个赋值:


SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

 这样不行,因为这里的createtime是string的,不能这样写(如果createtime属性是Date类型,给String类型的createtimeStr 属性赋值 可以这样写,createtimeStr这个字段值常用于前端展示~ 不用再在前端处理),写了访问报500错:

 那就这样写:

    public String getCreatetime() {
        try {
            return DateUtils.getStringDate(DateUtils.stringToDate(createtime, DateUtil.GENERAL_PATTERN_2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return createtime;
    }

 

 重要工具类DateUtils.java具体代码如下

  

 

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

    private static final int BIN_DIGITS = 12;

    public static final short ONE_MS = 1 << BIN_DIGITS;

    public static String getDatePattern() {
        return "yyyy-MM-dd";
    }

    public static String getTimePattern() {
        return "yyyy-MM-dd HH:mm:ss";
    }

    public static String getDatePattern8() {
        return "yyyyMMdd";
    }

    public static String getTimePattern14() {
        return "yyyyMMddHHmmss";
    }

    public static Date getDateTime(Date date, String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        String strDate = df.format(date);
        Calendar cal = new GregorianCalendar();
        cal.setTime(stringToDate(strDate, format));
        return cal.getTime();
    }

    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getYear(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }

    public static int getMonth(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    public static int getDayOfMonth() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }

    public static int getDayOfMonth(Date date) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    public static final Date stringToDate(String strDate, String format) throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(format);

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return (date);
    }

    public static Date stringToDate(String strDate) throws ParseException {
        Date aDate = null;
        try {
            aDate = stringToDate(strDate, getDatePattern());
        } catch (ParseException pe) {
            pe.printStackTrace();
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return aDate;
    }

    public static final String dateToString(Date aDate, String format) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            //
        } else {
            df = new SimpleDateFormat(format);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    public static final String getStringDate(Date date) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        return format1.format(date.getTime());
    }

    public static final String dateToString(Date aDate) {
        return dateToString(aDate, getDatePattern());
    }

    public static final String timeToString(Date aDate) {
        return dateToString(aDate, getTimePattern());
    }

    public static Date getDate(Date aDate) throws ParseException {
        return getDateTime(aDate, getDatePattern());
    }

    public static Date getTime(Date aDate) throws ParseException {
        return getDateTime(aDate, getTimePattern());
    }

    public static long getDaysBetween(Date dtFrom, Date dtEnd) {
        long begin = dtFrom.getTime();
        long end = dtEnd.getTime();
        long inter = end - begin;
        if (inter < 0) {
            inter = inter * (-1);
        }
        long dateMillSec = 24 * 60 * 60 * 1000;

        long dateCnt = inter / dateMillSec;

        long remainder = inter % dateMillSec;

        if (remainder != 0) {
            dateCnt++;
        }
        return dateCnt;
    }

    public static long getMinsBetween(Date dtFrom, Date dtEnd) {
        long begin = dtFrom.getTime();
        long end = dtEnd.getTime();
        long inter = end - begin;
        return inter / 60000;
    }

    public static String getLastDateOfyearAndMonth(String yearAndMonth) throws Exception {
        String start = yearAndMonth + "01";
        Date startDate = DateUtils.stringToDate(start, "yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String end = DateUtils.dateToString(calendar.getTime(), "yyyyMMdd");
        return end;
    }

    public static String getNextMonthString(String strDate, String format) throws ParseException {
        Date date = stringToDate(strDate, format);
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        return dateToString(cal.getTime(), format);
    }

    /**
     * 获取上个月的最后一天
     * 
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getLastDayOfLastMonth(Date date) throws ParseException {

        String str_firstDay = dateToString(date, "yyyyMM") + "01";
        Date date_firstDay = stringToDate(str_firstDay, "yyyyMMdd");
        if (date_firstDay != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date_firstDay);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            return cal.getTime();
        }
        return null;
    }

    /**
     * 获取上个月的最后一天
     * 
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getLastDayOfLastMonth(Date date, String format) throws ParseException {

        return dateToString(getLastDayOfLastMonth(date), format);
    }

    /**
     * 获取传入时间所在月的最后一天
     * 
     * @param date
     * @return
     */
    public static int getLastDay(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        return calendar.get(Calendar.DAY_OF_MONTH);

    }

    /**
     * 获取指定时间下个月的第一天零时00:00:00
     * 
     * @param date
     * @return
     */
    public static Date getNextMonthFirstDay(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static Date getMonthLastDay(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取一个月的开始时间
     * 
     * @param date
     * @return
     */
    public static Date getMonthFirstDay(Date date) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 当前时间是否在抽奖的有效时间内
     * 
     * @return
     */
    public static boolean raffleEffectiveTime() {
        boolean effective = false;
        try {
            Date begin = DateUtils.stringToDate("20130701000000", "yyyyMMddHHmmss");
            Date end = DateUtils.stringToDate("20131001000000", "yyyyMMddHHmmss");
            Date current = new Date();

            if (current.compareTo(begin) == 1 && current.compareTo(end) == -1) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return effective;

    }

    /**
     * 将源格式字符串日期转换成目标格式字符串日期
     * 
     * @param origStrDate 字符串日期
     * @param origFormat 源格式
     * @param destFormat 目标格式
     * @return
     * @throws ParseException
     */
    public static final String dateStrToString(String origStrDate, String origFormat, String destFormat) throws ParseException {
        Date origDate = stringToDate(origStrDate, origFormat);
        return dateToString(origDate, destFormat);
    }

    // 季度一年四季, 第一季度:2月-4月, 第二季度:5月-7月, 第三季度:8月-10月, 第四季度:11月-1月
    private static int getQuarterInMonth(int month, boolean isQuarterStart) {
        int months[] = { 1, 4, 7, 10 };
        if (!isQuarterStart) {
            months = new int[] { 3, 6, 9, 12 };
        }
        if (month >= 2 && month <= 4)
            return months[0];
        else if (month >= 5 && month <= 7)
            return months[1];
        else if (month >= 8 && month <= 10)
            return months[2];
        else
            return months[3];
    }

    // 获得本周一与当前日期相差的天数
    public static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    // 获得当前周- 周一的日期
    public static Date getCurrentMonday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getThisWeekMonday(new Date()));
        cal.add(Calendar.DATE, -7);
        return cal.getTime();
    }

    public static Date getThisWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获得当月起始时间
     * 
     * @return
     */
    public static Date getStartMounth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当前季度 起始时间
     * 
     * @return
     */
    public static Date getStartQuarter(Date date) {
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 获取当年起始时间
     */
    public static Date getStartYear(Date date) {
        Calendar currCal = Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取某年第一天日期
     * 
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    public static void main(String[] args) throws ParseException {

//         boolean effective=false;
//            try {
//                Date d=getMonthFirstDay(new Date());
//                System.out.println(DateUtils.dateToString(d, "yyyyMMddHHmmsswww"));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        Date receiveTime = DateUtils.stringToDate("20131226", "yyyyMMdd");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(receiveTime);

        int week = calendar.get(Calendar.DAY_OF_WEEK);
        if (week < Calendar.WEDNESDAY) {
            calendar.add(Calendar.DATE, -14);
        } else {
            calendar.add(Calendar.DATE, -7);
        }
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
        Date effectiveStart = calendar.getTime();
        Date effectiveEnd = receiveTime;

        System.out.println(DateUtils.dateToString(effectiveStart, "yyyyMMddHHmmsswww"));
        System.out.println(DateUtils.dateToString(effectiveEnd, "yyyyMMddHHmmsswww"));
    }

    /**
     * 获取两个时间秒差数(带毫秒数)
     * 
     * @param dtFrom
     * @param dtEnd
     * @return
     */
    public static double getSecsBetweenD(Date dtFrom, Date dtEnd) {
        BigDecimal begin = new BigDecimal(dtFrom.getTime());
        BigDecimal end = new BigDecimal(dtEnd.getTime());
        BigDecimal inter = end.subtract(begin);
        return inter.divide(new BigDecimal("1000")).doubleValue();
    }

}

 

posted @ 2020-07-28 16:52  淘来的星湖  阅读(1428)  评论(0编辑  收藏  举报