操作时间的工具类

来源:互联网 发布:2016全国各地上牌数据 编辑:程序博客网 时间:2024/06/07 23:06
public class DateUtil {    private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";    /**     * 判断时间字符串是否是在当前天以及当前天之后     * 先将时间字符串转换成Date     * 获取当前天的日期     */    public static boolean isCurAndNextDay(String str, String format) {        boolean isCurAndNext=false;        Date date = str2Date(str,format);        Date curdate =getCurrentDate();        long time=date.getTime();        long curtime=curdate.getTime();        if(curtime>=time){            isCurAndNext=false;        }else{            isCurAndNext=true;        }        return isCurAndNext;    }    /**     * 判断当前是一个星期中的第几天     * 第一天是星期一 最后一天是星期日     */    public static int getWeekOfCurDate() {        int w=0;        Date curdate =getCurrentDate();        Calendar cal = Calendar.getInstance();        cal.setTime(curdate);        w=cal.get(Calendar.DAY_OF_WEEK)-2;        if(w<0){            w=6;        }        return w;    }    /**     * 将时间字符串转换成Date     */    public static Date str2Date(String str, String format) {        if (str == null || str.length() == 0) {            return null;        }        if (format == null || format.length() == 0) {            format = FORMAT;        }        Date date = null;        try {            SimpleDateFormat sdf = new SimpleDateFormat(format);            date = sdf.parse(str);        } catch (Exception e) {            e.printStackTrace();        }        return date;    }    public static Date str2Date(String str) {        return str2Date(str, null);    }    /**     * 将时间字符串转换成Calendar     *     * @param str     * @param format     * @return     */    public static Calendar str2Calendar(String str, String format) {        Date date = str2Date(str, format);        if (date == null) {            return null;        }        Calendar c = Calendar.getInstance();        c.setTime(date);        return c;    }    public static Calendar str2Calendar(String str) {        return str2Calendar(str, null);    }    public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss        return date2Str(c, null);    }    public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss        return date2Str(d, null);    }    public static String date2Str(Calendar c, String format) {        if (c == null) {            return null;        }        return date2Str(c.getTime(), format);    }    public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss        if (d == null) {            return null;        }        if (format == null || format.length() == 0) {            format = FORMAT;        }        SimpleDateFormat sdf = new SimpleDateFormat(format);        String s = sdf.format(d);        return s;    }    /**     * 获得当前日期的字符串格式     * 2016-05-01     *     * @return     */    public static String getCurDateStr() {        Calendar c = Calendar.getInstance();        c.setTime(new Date());        return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"                + c.get(Calendar.DAY_OF_MONTH) + "-"                + c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)                + ":" + c.get(Calendar.SECOND);    }    /**     * 获得当前日期的字符串格式     */    public static String getCurDateStr(String format) {        Calendar c = Calendar.getInstance();        return date2Str(c, format);    }    /**     * 获得当前日期的字符串格式,格式到秒     *     * @return time -> yyyy-MM-dd-HH-mm-ss     */    public static String getMillon(long time) {        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);    }    /**     * 格式到天     *     * @return time -> yyyy-MM-dd     */    public static String getDay(long time) {        return new SimpleDateFormat("yyyy-MM-dd").format(time);    }    /**     * 格式到毫秒     *     * @return time -> yyyy-MM-dd-HH-mm-ss-SSS     */    public static String getSMillon(long time) {        return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);    }    /**     * 输入的是String,格式诸如20120102,实现加一天的功能,返回的格式为String,诸如20120103     */    public static String strAddOneDay(String str) throws ParseException {        String year = str.substring(0, 4);        String month = str.substring(4, 6);        String day = str.substring(6);        String date1 = year + "-" + month + "-" + day;        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        Date startDate = sdf.parse(date1);        Calendar cd = Calendar.getInstance();        cd.setTime(startDate);        cd.add(Calendar.DATE, 1);        String dateStr = sdf.format(cd.getTime());        String year1 = dateStr.substring(0, 4);        String month1 = dateStr.substring(5, 7);        String day1 = dateStr.substring(8);        return year1 + month1 + day1;    }    /**     * 输入的是String,格式诸如20120102,实现减一天的功能,返回的格式为String,诸如20120101     */    public static String strDecreaseOneDay(String row) throws ParseException {        String year = row.substring(0, 4);        String month = row.substring(4, 6);        String day = row.substring(6);        String date1 = year + "-" + month + "-" + day;        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        Date startDate = sdf.parse(date1);        Calendar cd = Calendar.getInstance();        cd.setTime(startDate);        cd.add(Calendar.DATE, -1);        String dateStr = sdf.format(cd.getTime());        String year1 = dateStr.substring(0, 4);        String month1 = dateStr.substring(5, 7);        String day1 = dateStr.substring(8);        return year1 + month1 + day1;    }    /**     * 输入的格式为String,诸如20120101,返回的格式为String,诸如2012-01-01     */    public static String stringDateChange(String date) {        if (date.length() == "20120101".length()) {            String year = date.substring(0, 4);            String month = date.substring(4, 6);            String day = date.substring(6);            return year + "-" + month + "-" + day;        } else {            return date;        }    }    /**     * 获取当前时间的Date     *     * @return     */    public static Date getCurrentDate() {        Date date = new Date(getCurrentTime());        return date;    }    /**     * 获取当前时间的毫秒数     *     * @return     */    public static long getCurrentTime() {        return System.currentTimeMillis();    }    /**     * 获取昨天 Data     *     * @param date     * @return     */    public static Date getLastdayDate(Date date) {        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.add(Calendar.DATE, -1);        return calendar.getTime();    }    /**     * 获取当前时间的相差多天的日期集合 如;当前日期之前15天内的日期     *     * @param day 正 负 值     * @return     */    public static List<Date> getDiffDayDate(int day) {        List<Date> list = new ArrayList<>();        for (int i = 0; i < Math.abs(day); i++) {            Calendar calendar = Calendar.getInstance();            calendar.setTime(getCurrentDate());            if (day > 0) {                calendar.add(Calendar.DATE, (i + 1));            } else {                calendar.add(Calendar.DATE, -(i + 1));            }            list.add(calendar.getTime());        }        return list;    }    /**     * 获取明天Date     *     * @param date     * @return     */    public static Date getNextdayDate(Date date) {        Calendar calendar = Calendar.getInstance();        calendar.setTime(date);        calendar.add(Calendar.DATE, 1);        return calendar.getTime();    }    /**     * 判断是否是同一天     *     * @param one     * @param another     * @return     */    public static boolean isTheSameDay(Date one, Date another) {        Calendar calendar = Calendar.getInstance();        calendar.setTime(one);        Calendar calendar2 = Calendar.getInstance();        calendar2.setTime(another);        int oneDay = calendar.get(Calendar.DAY_OF_YEAR);        int anotherDay = calendar2.get(Calendar.DAY_OF_YEAR);        return oneDay == anotherDay;    }
0 0