java DateUtils

package cn.com.icreate.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间相关操作
 * 
 * @author Satan
 *
 */
public class DateUtils {

    public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 得到一个日期的时间戳
     */
    public long getTimeStampByDate(String date) {

        Date cdate = null;
        try {
            cdate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return cdate.getTime();
    }

    /**
     * 返回当前时间的时间戳
     * @return
     */
    public static String getCurrentStamp(){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String datelong = simpleDateFormat.format(date);
        return datelong;
    }



    /**
     * 将日期转化为字符串
     * 
     */
    public String getDateStringByDate(Date date) {
        String cdate = simpleDateFormat.format(date);
        return cdate;
    }

    /**
     * 返回该日期的后n天 YYYY-MM-DD
     * @param s YYYY-MM-DD
     * @param n int
     * @return YYYY-MM-DD
     */
    public String addDay(String s, int n) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            Calendar cd = Calendar.getInstance();
            cd.setTime(sdf.parse(s));
            cd.add(Calendar.DATE, n);// 增加一天

            return sdf.format(cd.getTime());

        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据日期转为时间戳进行比较,并返回差值
     * 
     */
    public long getTimeDifference(String date, String preDate) {

        long datestamp = getTimeStampByDate(date);
        long predatestamp = getTimeStampByDate(preDate);

        return datestamp - predatestamp;
    }

    /**
     * 计算两个日期之间相差的天数
     * 
     * @param smdate
     *            较小的时间
     * @param bdate
     *            较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public int daysBetween(String smdate, String bdate) {
        Date smalldate=new Date();
        try {
            smalldate = simpleDateFormat.parse(smdate);
        } catch (ParseException e) {
        }
        Date bigdate=new Date();
        try {
            bigdate = simpleDateFormat.parse(bdate);
        } catch (ParseException e) {
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smalldate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bigdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        int i=Integer.parseInt(String.valueOf(between_days));
        return i;
    }

    /**
     * 
     * 判断日期是否为周六日
     * 如果为周六日,则返回1,否则返回0
     * @param date
     * @return
     */
    public int isWeekend(String date) {

        int is_weekend = 0;

        Date bdate = null;
        try {
            bdate = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(bdate);

        // 如果为周六日,则返回1,否则返回0
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            is_weekend = 1;
        }
        return is_weekend;
    }
    /**
     * 获取当前日期
     * @return
     */
    public String getNowDate(){
        Date date=new Date();
        return getDateStringByDate(date);
    }

    public String dateForString(String time){
        if(time!=null&&!time.equals("")){

            Date cdate = null;
            try {
                cdate = simpleDateFormat.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            time=getDateStringByDate(cdate);
        }
        return time;
    }
    public String getNowYear(){
        return getNowDate().substring(0,4);
    }
    /**
     * 获取当前周开始日期(周一)
     * @param date   YYYY-MM-DD
     * @return  YYYY-MM-DD
     * @throws ParseException 
     */
    public String getWeekStart(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd = Calendar.getInstance();
        cd.setTime(sdf.parse(date));
        int day = cd.get(Calendar.DAY_OF_WEEK)-1;
        if(day == 1){
        } else if(day == 2){
            cd.add(Calendar.DATE, -1);
        } else if(day == 3){
            cd.add(Calendar.DATE, -2);
        } else if(day == 4){
            cd.add(Calendar.DATE, -3);
        } else if(day == 5){
            cd.add(Calendar.DATE, -4);
        } else if(day == 6){
            cd.add(Calendar.DATE, -5);
        } else if(day == 0){
            cd.add(Calendar.DATE, -6);
        }
        return sdf.format(cd.getTime());
    }
    /**
     * 获取当前周结束日期(周日)
     * @param date   YYYY-MM-DD
     * @return      YYYY-MM-DD
     * @throws ParseException 
     */
    public String getWeekEnd(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd = Calendar.getInstance();
        cd.setTime(sdf.parse(date));
        int day = cd.get(Calendar.DAY_OF_WEEK)-1;
        if(day == 1){
            cd.add(Calendar.DATE, 6);
        } else if(day == 2){
            cd.add(Calendar.DATE, 5);
        } else if(day == 3){
            cd.add(Calendar.DATE, 4);
        } else if(day == 4){
            cd.add(Calendar.DATE, 3);
        } else if(day == 5){
            cd.add(Calendar.DATE, 2);
        } else if(day == 6){
            cd.add(Calendar.DATE, 1);
        } else if(day == 0){
        }
        return sdf.format(cd.getTime());
    }

    /*
     * 判断年份是否是闰年
     */
    public boolean isRunYear(String year){
        if(Integer.valueOf(year) % 4 == 0 && Integer.valueOf(year) % 100 != 0 || Integer.valueOf(year) % 400 == 0){
            return true;
        }else{
            return false;
        }
    }

    /*
     * 根据年份和月份计算天数
     */
    public int getDaysByYearAndMonth(int year,int month){
        if(month==2){
            if (((year % 100 == 0) && (year % 400 == 0))
                || ((year % 100 != 0) && (year % 4 == 0))) {
                    return 29;
                }else {
                    return 28;
                }
        }else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 ) {
            return 31;
        }else if(month==4 || month==6 || month==9 || month==11) {
            return 30;
        }
        return 0;   
    }


    /**
     * 获取三周前开始日期(周一)
     * @param date   YYYY-MM-DD
     * @return  YYYY-MM-DD
     * @throws ParseException 
     */
    public String getBeforeWeekStart(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd = Calendar.getInstance();
        cd.setTime(sdf.parse(date));
        int day = cd.get(Calendar.DAY_OF_WEEK)-1;
        if(day == 1){
            cd.add(Calendar.DATE, -21);
        } else if(day == 2){
            cd.add(Calendar.DATE, -22);
        } else if(day == 3){
            cd.add(Calendar.DATE, -23);
        } else if(day == 4){
            cd.add(Calendar.DATE, -24);
        } else if(day == 5){
            cd.add(Calendar.DATE, -25);
        } else if(day == 6){
            cd.add(Calendar.DATE, -26);
        } else if(day == 0){
            cd.add(Calendar.DATE, -27);
        }
        return sdf.format(cd.getTime());
    }

    /**
     * 获取上上周开始日期(周一)
     * @param date   YYYY-MM-DD
     * @return  YYYY-MM-DD
     * @throws ParseException 
     */
    public String getBeforePWeekStart(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd = Calendar.getInstance();
        cd.setTime(sdf.parse(date));
        int day = cd.get(Calendar.DAY_OF_WEEK)-1;
        if(day == 1){
            cd.add(Calendar.DATE, -14);
        } else if(day == 2){
            cd.add(Calendar.DATE, -15);
        } else if(day == 3){
            cd.add(Calendar.DATE, -16);
        } else if(day == 4){
            cd.add(Calendar.DATE, -17);
        } else if(day == 5){
            cd.add(Calendar.DATE, -18);
        } else if(day == 6){
            cd.add(Calendar.DATE, -19);
        } else if(day == 0){
            cd.add(Calendar.DATE, -20);
        }
        return sdf.format(cd.getTime());
    }

    /**
     * 获取上周开始日期(周一)
     * @param date   YYYY-MM-DD
     * @return  YYYY-MM-DD
     * @throws ParseException 
     */
    public String getBeforeLastWeekStart(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd = Calendar.getInstance();
        cd.setTime(sdf.parse(date));
        int day = cd.get(Calendar.DAY_OF_WEEK)-1;
        if(day == 1){
            cd.add(Calendar.DATE, -7);
        } else if(day == 2){
            cd.add(Calendar.DATE, -8);
        } else if(day == 3){
            cd.add(Calendar.DATE, -9);
        } else if(day == 4){
            cd.add(Calendar.DATE, -10);
        } else if(day == 5){
            cd.add(Calendar.DATE, -11);
        } else if(day == 6){
            cd.add(Calendar.DATE, -12);
        } else if(day == 0){
            cd.add(Calendar.DATE, -13);
        }
        return sdf.format(cd.getTime());
    }



    /**
     * 判断是否为周一
     * @param date
     * @return
     * @throws ParseException
     */
    public Boolean isMonday(String date) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cd = Calendar.getInstance();
        cd.setTime(sdf.parse(date));
        int day = cd.get(Calendar.DAY_OF_WEEK)-1;
        if(day == 1){
            return true;
        } else 
            return false;
    }

    /**
     * 设置默认时间(三周前的星期一开始到三周前的星期日
     * @return
     */
    public List<String> setDefaultTime(){
        DateUtils du = new DateUtils();
        String nowTime = du.getNowDate();
        String defaultTimeStart = null;
        String defaultTimeEnd = null;
        String lastTimeStart = null;
        String lastTimeEnd = null;
        String pTimeStart = null;
        String pTimeEnd = null;
        List<String> timeList = new ArrayList<String>();
        try {
            //上周一
            lastTimeStart = du.getBeforeLastWeekStart(nowTime);
            timeList.add(lastTimeStart);
            //上周日
            lastTimeEnd = du.getWeekEnd(lastTimeStart);
            timeList.add(lastTimeEnd);
            //上上周一
            pTimeStart = du.getBeforePWeekStart(nowTime);
            timeList.add(pTimeStart);
            //上上周日
            pTimeEnd = du.getWeekEnd(pTimeStart);
            timeList.add(pTimeEnd);
            //前三周一
            defaultTimeStart = du.getBeforeWeekStart(nowTime);
            timeList.add(defaultTimeStart);
            //前三周日
            defaultTimeEnd = du.getWeekEnd(defaultTimeStart);
            timeList.add(defaultTimeEnd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeList;
    }

    /**
     * 判断当前时间是否小于传入时间
     * @param date1
     * @return boolean
     */
    public static boolean compare_date(String date1) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = new Date();
            if (dt2.getTime() > dt1.getTime()) {
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }


    /**
     * 判断日期为该年的第几周
     * @return
     */
    public static int isWeek(String today) throws ParseException{


         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
          Date date = format.parse(today);
          Calendar calendar = Calendar.getInstance();
          calendar.setFirstDayOfWeek(Calendar.MONDAY);
          calendar.setTime(date);
          int num=(calendar.get(Calendar.WEEK_OF_YEAR));
          return num;
          }


}

这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值