zl程序教程

您现在的位置是:首页 >  Java

当前栏目

硬核!万人围观,Java时间工具类,无法拒绝

2023-03-14 23:00:10 时间

第一种: 日历工具类

package com.baidu.base.util;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 *
 * @Title: CalendarUtil
 * @Description: 日历工具类
 */
public class CalendarUtil {
 
    /**
     * 
     * @Description 获得指定日期工作周的指定星期几的日期
     * @Title getDateByWeekToDate
     * @param date
     * @param week
     * @return
     */
    public static Date getDateByWeekToDate(Date date, int week) {
        if (week <= 0) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_WEEK, week);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
    /**
     * @Description 得到按当前日期往前推一个月格式化时间格式为 00:00:00
     * @Title getUpperMonthTheToDay
     * @param date
     * @return
     */
    public static Date getUpperMonthTheToDayHHMMSS(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        return c.getTime();
    }
    /**
     * 
     * @Description 获得指定日期的最初时间 最初时间为00:00:00
     * @Title getDayBegin
     * @param date
     * @return
     */
    public static Date getDayBegin(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
 
    /**
     * @Description 获得指定日期的最终时间 最终时间为23:59:59
     * @Title getDayEnd
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
 
    /**
     * 
     * @Description 得到指定日期的天数范围, day可以为正/负数,正数为向后推day天,负数为向前推day天,不含当天
     * @Title getDateByDayToDate
     * @param date
     * @param day
     * @return
     */
    public static Date getDateByDayToDate(Date date, int day) {
        if (day == 0) {
            return date;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }
 
    /**
     *
     * @Description 获得本周的第一天,周一
     * @Title getWeekDayStartTime
     * @param date
     * @return
     */
    public static Date getWeekDayStartTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);//设置周一
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
//        c.setFirstDayOfWeek(Calendar.MONDAY);
        return c.getTime();
 
    }
 
    /**
     * @Description 获得本周的最后一天,周日
     * @Title getWeekDayEndTime
     * @param date
     * @return
     */
    public static Date getWeekDayEndTime(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        return c.getTime();
    }
    
    /**
     * @Description 得到指定年月的第一天
     * @Title getStartDayByYearMonth
     * @param year
     * @param month
     * @return
     */
    public static Date getBeginDayByYearMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(year, month-1, 1);
        return getDayBegin(c.getTime());
    }
    
    /**
     * @Description 得到指定年月的最后一天
     * @Title getEndDayByYearMonth
     * @param year
     * @param month
     * @return
     */
    public static Date getEndDayByYearMonth(int year, int month) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(year, month-1, 1);
        int endDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, endDay);
        return getDayEnd(c.getTime());
    }
    
    /**
     * @Description 得到按当前日期往前推一个月
     * @Title getUpperMonthTheToDay
     * @param date
     * @return
     */
    public static Date getUpperMonthTheToDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        c.add(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }
    
    /**
     * 
     * @Description 得到一年的第一天
     * @Title getStartYear
     * @param year
     * @return
     */
    public static Date getBeginYear(int year) {
        Calendar c = Calendar.getInstance();
        c.set(year, 0, 1, 0, 0, 0);
        return c.getTime();
    }
    
    /**
     * @Description 得到一年的最后一天
     * @Title getEndYear
     * @param year
     * @return
     */
    public static Date getEndYear(int year) {
        Date date = getBeginYear(year);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, 1);
        c.set(Calendar.SECOND, -1);
        return c.getTime();
    }
    
    /**
     * @Description 得到当前时间的年份
     * @Title getYear
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }
    
    /**
     * @Description 得到当前时间的月份
     * @Title getMonth
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }
    
    /**
     * 
     * @Description 得到date的差别日期  
     * @Title getDifferenceDay
     * @param date 日期
     * @param day 差别天数,可以为正,可以为-
     * @return
     */
    public static Date getDifferenceDay(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
    }
 
    
    public static void main(String[] arge) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = getDifferenceDay(new Date(), -1);
        
        System.out.println(sdf.format(date));
    }
}

第二种:日期处理类

package com.baidu.base.util;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 *
 * @Title: DateUtil
 * @Description: 日期处理w
 */
public class DateUtil {
    
    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
    private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
     /**
     * 标准的日期时间字符串格式
     */
    public static final String STANDARD_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * 标准的日期字符串格式
     */
    public static final String STANDARD_DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 获取YYYY格式
     * @return
     */
    public static String getSdfTimes() {
        return sdfTimes.format(new Date());
    }
    
    /**
     * 获取YYYY格式
     * @return
     */
    public static String getYear() {
        return sdfYear.format(new Date());
    }
 
    /**
     * 获取YYYY-MM-DD格式
     * @return
     */
    public static String getDay() {
        return sdfDay.format(new Date());
    }
    
    /**
     * 获取YYYYMMDD格式
     * @return
     */
    public static String getDays(){
        return sdfDays.format(new Date());
    }
 
    /**
     * 获取YYYY-MM-DD HH:mm:ss格式
     * @return
     */
    public static String getTime() {
        return sdfTime.format(new Date());
    }
 
    /**
    * @Title: compareDate
    * @Description: TODO(日期比较,如果s>=e 返回true 否则返回false)
    * @param s
    * @param e
    * @return boolean  
    * @throws
    * @author fh
     */
    public static boolean compareDate(String s, String e) {
        if(fomatDate(s)==null||fomatDate(e)==null){
            return false;
        }
        return fomatDate(s).getTime() >=fomatDate(e).getTime();
    }
 
    /**
     * 格式化日期
     * @return
     */
    public static Date fomatDate(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 校验日期是否合法
     * @return
     */
    public static boolean isValidDate(String s) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            fmt.parse(s);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            return false;
        }
    }
    
    /**
     * @param startTime
     * @param endTime
     * @return
     */
    public static int getDiffYear(String startTime,String endTime) {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        try {
            //long aa=0;
            int years=(int) (((fmt.parse(endTime).getTime()-fmt.parse(startTime).getTime())/ (1000 * 60 * 60 * 24))/365);
            return years;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
            return 0;
        }
    }
     
    /**
     * <li>功能描述:时间相减得到天数
     * @param beginDateStr
     * @param endDateStr
     * @return
     * long 
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr,String endDateStr){
        long day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate = null;
        java.util.Date endDate = null;
        
            try {
                beginDate = format.parse(beginDateStr);
                endDate= format.parse(endDateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            day=(endDate.getTime()-beginDate.getTime())/(24*60*60*1000);
            //System.out.println("相隔的天数="+day);
      
        return day;
    }
    
    /**
     * 得到n天之后的日期
     * @param days
     * @return
     */
    public static String getAfterDayDate(String days) {
        int daysInt = Integer.parseInt(days);
        
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        
        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdfd.format(date);
        
        return dateStr;
    }
    
    /**
     * 得到n天之后是周几
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }
    
    
    /**
     * 获取当前时间
     *
     * @return Date
     */
    public static Date getCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat(STANDARD_DATE_FORMAT);
        try {
            return format.parse(format.format(new Date()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new Date();
    }
    
    /**
     * 获取当前时间     yyyy-MM-dd HH:mm:ss格式
     *
     * @return Date
     */
    public static Date getCurrentTimes() {
        SimpleDateFormat format = new SimpleDateFormat(STANDARD_DATETIME_FORMAT);
        try {
            return format.parse(format.format(new Date()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new Date();
    }
    
    public static Date convertStringToDate(String dateString, String format) {
 
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date date = null;
 
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            // do nothing
        }
 
        return date;
    }
    
    /**
     * 对日期对象进行天数加减
     *
     * @param date 日期对象
     * @param days 天数,如果要将日期向前推算则使用负数
     * @return Date
     */
    public static Date addDays(Date date, int days) {
 
        Date newDate = new Date(date.getTime() + (long) days * 24 * 60 * 60 * 1000);
        return newDate;
    }
    public static void main(String[] args) {
        System.out.println(getDays());
        System.out.println(getAfterDayWeek("3"));
    }
 
}