JAVA-常见的时间处理方法

/**
 * 获取 当前年、半年、季度、月、日、小时 开始结束时间
 */
private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private final static String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

 //获取当前时间:
Calendar calendar = Calendar.getInstance();

calendar.setFirstDayOfWeek(Calendar.MONDAY);

//当前周

int weekId = calendar.get(Calendar.WEEK_OF_YEAR);

//当前年

int yearId = calendar.get(Calendar.YEAR);

//当前年

int monthId = calendar.get(Calendar.MONTH )+1;


/**
 * 获得本天的结束时间
 *
 * @return
 */
public static Date getDayEndTime(Date date) {
    try {
        return longSdf.parse(shortSdf.format(date) + " 23:59:59");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

/**
 * 获取日期的星期数
 * @param dt
 * @return
 */
public static String getWeekOfDate(Date dt) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(dt);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
    if (w < 0)
        w = 0;
    return weekDays[w];
}

/**
 * 获取日期的星期数
 * @param date
 * @return
 */
public static String getDay(Date date) {
    if(date == null) {
        return null;
    }
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    int day = calendar.get(Calendar.DAY_OF_WEEK);
    switch (day) {
        case 1:
            return "周日";
        case 2:
            return "周一";
        case 3:
            return "周二";
        case 4:
            return "周三";
        case 5:
            return "周四";
        case 6:
            return "周五";
        case 7:
            return "周六";
    }
    return null;
}
/**
     * 获取日期的星期数
     * @param dt
     * @return
     */
    public static int getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(w==0){
            w = 7;
        }
        return w;
    }
    /**
     * 获取日期的小时数
     * @param dt
     * @return
     */
    public static int getHourOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.HOUR_OF_DAY);
        return w;
    }
    /**
     * 获取日期的分钟数
     * @param dt
     * @return
     */
    public static int getMinuteOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.MINUTE);
        return w;
    }


/**
 * 将输入的时间转换为相对当前时刻的时间。
 * @return
 */
public static String getShortTime(Date date) {
    String shortString = null;
    long now = Calendar.getInstance().getTimeInMillis();
    if(date == null) return shortString;
        long delTime = (now - date.getTime())/1000;
    if(delTime > 365*24*60*60) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        return dateFormat.format(date.getTime());
    } else if(delTime > 7*24*60*60) {
        shortString = (int)(delTime/(7*24*60*60)) + "周前";
    } else if(delTime > 24*60*60) {
        shortString = (int)(delTime/(24*60*60)) + "天前";
    } else if(delTime > 60*60) {
        shortString = (int)(delTime/(60*60)) + "小时前";
    } else if(delTime > 60) {
        shortString = (int)(delTime/(60)) + "分钟前";
    } else if(delTime > 10) {
        shortString = delTime + "秒前";
    } else {
        shortString = "刚刚";
    }
    return shortString;
}
/**
 * 将输入的时间转换为相对当前时刻的时间。
 * @return
 */
public static String dateToLocalString(Date date) {
    long reduce = new Date().getTime() - date.getTime();
    if(reduce <= 600000) {
        if(reduce <= 60000) {
            return reduce/1000 + "秒前";
        } else {
            return (reduce / 60000) + "分钟前";
        }
    }
    Calendar now = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if(calendar.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
        if(calendar.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
            if(calendar.get(Calendar.DATE) == now.get(Calendar.DATE)) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("今天 HH:mm");
                return dateFormat.format(calendar.getTime());
            }
            if(calendar.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("昨天 HH:mm");
                return dateFormat.format(calendar.getTime());
            }
            if(calendar.get(Calendar.DATE) == now.get(Calendar.DATE) - 2) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("前天 HH:mm");
                return dateFormat.format(calendar.getTime());
            }
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
        return dateFormat.format(calendar.getTime());
    } else {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        return dateFormat.format(calendar.getTime());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值