博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
日期工具类
阅读量:6250 次
发布时间:2019-06-22

本文共 6780 字,大约阅读时间需要 22 分钟。

hot3.png

package com.bz.nwvisitor.common.utils;import com.bz.common.lang.StringUtil;import java.math.BigDecimal;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.TimeZone;public class CalendarUtil extends com.taobao.util.CalendarUtil {    private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {        return new SimpleDateFormat(parttern);    }    public static String stringToDate(Date date, String parttern) {        String myDate = null;        if (date != null) {            try {                myDate = getDateFormat(parttern).format(date);            } catch (Exception e) {            }        }        return myDate;    }    public static Date setDateTime(Date date) {        if (date == null) {            return null;        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.set(Calendar.SECOND, 0);        calendar.set(Calendar.MILLISECOND, 0);        return calendar.getTime();    }    public static Date setBeginTime(Date date, int hour, int minute) {        if (date == null) {            return null;        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.set(Calendar.HOUR_OF_DAY, hour);        calendar.set(Calendar.MINUTE, minute);        calendar.set(Calendar.SECOND, 0);        calendar.set(Calendar.MILLISECOND, 0);        return calendar.getTime();    }    public static Date getBeginOfMinute(Date date) {        if (date == null) {            return null;        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.set(Calendar.SECOND, 0);        calendar.set(Calendar.MILLISECOND, 0);        return calendar.getTime();    }    public static Date getEndOfMinute(Date date) {        if (date == null) {            return null;        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.set(Calendar.SECOND, 59);        calendar.set(Calendar.MILLISECOND, 999);        return calendar.getTime();    }    public static Date getEndOfDay(Date date) {        if (date == null) {            return null;        }        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.set(Calendar.HOUR_OF_DAY, 23);        calendar.set(Calendar.MINUTE, 59);        calendar.set(Calendar.SECOND, 59);        calendar.set(Calendar.MILLISECOND, 999);        return calendar.getTime();    }    public static boolean isTimeBetween(Date date, String startTime, String endTime) {        Date startDate = CalendarUtil.toDate(startTime, "HH:mm");        Date endDate = CalendarUtil.toDate(endTime, "HH:mm");        Date cdata = CalendarUtil.toDate(CalendarUtil.toString(date, "HH:mm"), "HH:mm");        return startDate.getTime() <= cdata.getTime() && cdata.getTime() <= endDate.getTime();    }    /**     * @param sDate     * @param eDate     * @return result返回结果(0--相同 1-->前者大 2--
<后者大) * public static int getcomparedate(date sdate, date edate) { result="sC.compareTo(eC);" 将开始时间赋给日历实例 calendar sc="Calendar.getInstance();" sc.settime(sdate); 将结束时间赋给日历实例 ec="Calendar.getInstance();" ec.settime(edate); 比较 返回结果 return result; } getdaysbetween(date startdate, enddate) startdate="zerolizedTime(startDate);" if (enddate.gettime()>
startDate.getTime()) { endDate = zerolizedTime(addDate(endDate, 1)); } return getIntervalDays(startDate, endDate); } public static Date getDateTime(Date bookDate, String startDate) { if (!StringUtil.isBlank(startDate)) { String[] split = startDate.split(":"); if (split.length > 0) { Date bk = CalendarUtil.setBeginTime(bookDate, Integer.parseInt(split[0]), Integer.parseInt(split[1])); return bk; } } return null; } /** * 字符串转换成日期 * * @param str * @return date */ public static Date setDate(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = format.parse(str); } catch (Exception e) { } return date; } public static Date setDateTime(String str) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date date = null; try { date = format.parse(str); } catch (Exception e) { } return date; } public static Calendar setCalendar(Date date) { Calendar cal = Calendar.getInstance(); cal.setTime(date); return cal; } public static boolean areSameDay(Date dateA, Date dateB) { Calendar calDateA = Calendar.getInstance(); calDateA.setTime(dateA); Calendar calDateB = Calendar.getInstance(); calDateB.setTime(dateB); if (calDateB.get(Calendar.HOUR_OF_DAY) == 0 && calDateB.get(Calendar.MINUTE) == 0) { calDateB.add(Calendar.DAY_OF_MONTH, -1); } return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR) && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH) && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB.get(Calendar.DAY_OF_MONTH); } public static Date getLimitTime(Integer limit) { String xiujia = "09-03|09-04|09-05|10-01|10-02|10-05|10-06|10-07|10-10"; String gongzuo = "09-06"; int count = 0; Date date = new Date(); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); while (limit > count) { calendar.add(Calendar.DATE, 1); SimpleDateFormat monthDay = new SimpleDateFormat("MM-dd"); String str = monthDay.format(calendar.getTime()); if (xiujia.indexOf(str) >= 0) { continue; // calendar.add(Calendar.DATE, 1); } if (gongzuo.indexOf(str) >= 0) { count++; } if (!(Calendar.SUNDAY == calendar.get(Calendar.DAY_OF_WEEK) || Calendar.SATURDAY == calendar.get(Calendar.DAY_OF_WEEK))) { count++; } } return calendar.getTime(); } public static String stringInsert(Date date, String b, Integer t) { TimeZone time = TimeZone.getTimeZone("GMT+8"); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.setTimeZone(time); calendar.add(Calendar.HOUR, -8); String a = stringToDate(calendar.getTime(), "yyyyMMddHHmmss"); return a.substring(0, t) + b + a.substring(t, a.length()) + "Z"; } public static String getHour(Date start, Date end, Integer pr) { long l = start.getTime() - end.getTime(); long day = l / (24 * 60 * 60 * 1000); double hour = (l / (60 * 60 * 1000) - day * 24); long min = (long) ((l / (60 * 1000)) - day * 24 * 60 - hour * 60); if (min == 30) { hour = hour + 0.5; } double num = hour * pr; return String.valueOf(num); }}

 

 

 

转载于:https://my.oschina.net/u/185811/blog/877152

你可能感兴趣的文章
封装方法实现react更新元素示例
查看>>
windows 2003 IIS 配置支持 CGI
查看>>
mysql 多线程写入后查询丢失数据的一个bug
查看>>
SQLIOSim 模拟SQLServer的行为来测试IO性能
查看>>
更改activity组件切换的动画
查看>>
周鸿祎在360新员工入职培训上的讲话
查看>>
网页基础编程第十章
查看>>
centos7 命令行版本 安装 teamviewer
查看>>
修复XP注册表文件system损坏故障
查看>>
每周一书
查看>>
Java代码规范那些事
查看>>
我的友情链接
查看>>
如何更好地对齐分区??
查看>>
使用Python从rds上下载mysql备份文件
查看>>
react native组件的创建
查看>>
批量删除文件
查看>>
Linux网络管理
查看>>
iOS JSPatch 热修复使用
查看>>
某二级行机房搬迁
查看>>
基于MVC+EasyUI的Web开发框架经验总结(4)--使用图表控件Highcharts
查看>>