博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java中Date类型的工具类
阅读量:5261 次
发布时间:2019-06-14

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

package com.mytripod.util;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;/** * @author Mytripod * @create 2018-09-30 21:25 */public class DateUtil {    public static String DatePattern = "yyyy-MM-dd";    public static String DateTimePattern = "yyyy-MM-dd HH:mm";    public static Date parseDate(String source) {        return parse(source, DatePattern);    }    public static Date parseDateTime(String source) {        if(source.trim().length() == 10) source = source + " 00:00";        return parse(source, DateTimePattern);    }    public static Date parse(String source, String patten) {        if (StringUtil.isEmpty(source))            return null;        DateFormat format;        try {            format = new SimpleDateFormat(patten);        } catch (Exception e) {            format = new SimpleDateFormat(DateTimePattern);        }        Date date;        try {            date = format.parse(source);        } catch (Exception e) {            date = null;        }        return date;    }    public static String formatDate(Date date) {        return format(date, DatePattern);    }    public static String formatDateTime(Date date) {        return format(date, DateTimePattern);    }    public static String format(Date date, String patten) {        if (date == null)            return "";        DateFormat format;        try {            format = new SimpleDateFormat(patten);        } catch (Exception e) {            format = new SimpleDateFormat(DateTimePattern);        }        return format.format(date);    }    public static String getFormatBeforeOrAfterDate(int offset) {        return getFormatBeforeOrAfterDay(offset, DatePattern);    }    public static String getFormatBeforeOrAfterDateTime(int offset) {        return getFormatBeforeOrAfterDay(offset, DateTimePattern);    }    public static String getFormatBeforeOrAfterDay(int offset, String patten) {        return format(getBeforeOrAfterDay(offset), patten);    }    public static Date getBeforeOrAfterDay(int offset) {        if (offset == 0)            return new Date();        Calendar c = Calendar.getInstance();        c.add(Calendar.DATE, offset);        return c.getTime();    }    public static int getYear() {        return getYear(new Date());    }    public static int getMonth() {        return getMonth(new Date());    }    public static int getDayOfWeek() {        return getDayOfWeek(new Date());    }    public static int getDayOfMonth() {        return getDayOfMonth(new Date());    }    public static int getDayOfYear() {        return getDayOfYear(new Date());    }    public static int getYear(Date date) {        return get(date, Calendar.YEAR);    }    public static int getMonth(Date date) {        return get(date, Calendar.MONTH) + 1;    }    public static int getDayOfWeek(Date date) {        int day = get(date, Calendar.DAY_OF_WEEK);        return day == 0 ? 7 : get(date, Calendar.DAY_OF_WEEK) - 1;    }    public static int getDayOfMonth(Date date) {        return get(date, Calendar.DAY_OF_MONTH);    }    public static int getDayOfYear(Date date) {        return get(date, Calendar.DAY_OF_YEAR);    }    private static Calendar getCalendar(Date date) {        Calendar c = Calendar.getInstance();        if (date != null)            c.setTime(date);        return c;    }    private static int get(Date date, int field) {        Calendar c = getCalendar(date);        return c.get(field);    }    public static boolean compareDateTime(String s1, String s2) {        return compare(parseDateTime(s1), parseDateTime(s2));    }    public static boolean compareDateTime(String s, Date date) {        return compare(parseDateTime(s), date);    }    public static boolean compareDateTime(Date date, String s) {        return compare(date, parseDateTime(s));    }    public static boolean compareDate(String s1, String s2) {        return compare(parseDate(s1), parseDate(s2));    }    public static boolean compareDate(String s, Date date) {        return compare(parseDate(s), date);    }    public static boolean compareDate(Date date, String s) {        return compare(date, parseDate(s));    }    public static boolean compare(String s1, String patten1, String s2,                                  String patten2) {        return compare(parse(s1, patten1), parse(s2, patten2));    }    public static boolean compare(String s, String patten, Date date) {        return compare(parse(s, patten), date);    }    public static boolean compare(Date date, String s, String patten) {        return compare(date, parse(s, patten));    }    public static boolean compare(Date d1, Date d2) {        if (d1 == null || d2 == null)            throw new IllegalArgumentException();        return d1.after(d2);    }    //获取两个日期的间隔天数    public static long getDiffDays (String strdate1,String strdate2){        long diff = 0;        try{            Date date1 = parseDate(strdate1);            Date date2 = parseDate(strdate2);            diff = date2.getTime() - date1.getTime();            diff = diff/1000/60/60/24;        } catch (Exception e) {            e.printStackTrace();        }        return diff;    }    //获取结算周期开始日期:xxxx-xx-26    public static String getFromDate(String date){        return getFromDate(parseDate(date));    }    public static String getFromDate(Date date){        String fromDate = "";        int year = getYear(date);        int month = getMonth(date);        int day = getDayOfMonth(date);        if (day <= 25) {            if(month == 1){                month =12;                year = year-1;            }            else month = month-1;        }        if(month < 10)            fromDate = year + "-0" + month + "-" + "26";        else            fromDate = year + "-" + month + "-" + "26";        return fromDate;    }    //获取结算周期结束日期xxxx-xx-01    public static String getEndDate(Date date){        String monthOfThis = getMonthOfThis(formatDate(date));        String endDate = monthOfThis.substring(0, 8)+"25";        return endDate;    }    public static String getMonthOfThis(String date){        String monthOfThis = "";        Date date_ = parseDate(date);        int year = getYear(date_);        int month = getMonth(date_);        int day = getDayOfMonth(date_);        if (day > 25) {             if(month == 12){                month =1;                year = year + 1;            }            else month = month+1;        }        if(month < 10)            monthOfThis = year + "-0" + month + "-" + "01";        else            monthOfThis = year + "-" + month + "-" + "01";        return monthOfThis;    }    /**     * 获得指定日期的后一天     * @param date     * @return     */    public static String getNextDay(String date){        Calendar c = Calendar.getInstance();        Date date_ = parseDate(date);        c.setTime(date_);        int day=c.get(Calendar.DATE);        c.set(Calendar.DATE,day+1);        return formatDate(c.getTime());    }    /**     * 获得指定日期的前一天     * @param date     * @return     */    public static String getlastDay(String date){        Calendar c = Calendar.getInstance();        Date date_ = parseDate(date);        c.setTime(date_);        int day=c.get(Calendar.DATE);        c.set(Calendar.DATE,day-1);        return formatDate(c.getTime());    }    /**     * 获得指定日期的前一个月     * @param date     * @return     */    public static String getlastMonth(String date){        Calendar c = Calendar.getInstance();        Date date_ = parseDate(date);        c.setTime(date_);        c.add(Calendar.MONTH, -1);        return formatDate(c.getTime());    }    /**     * 获得某日期相差指定天数的日期     * @param date     * @return     */    public static String getDateByN(String date,int n){        Calendar c = Calendar.getInstance();        Date date_ = parseDate(date);        c.setTime(date_);        int day=c.get(Calendar.DATE);        c.set(Calendar.DATE,day+n);        return formatDate(c.getTime());    }    public static void main(String arg[]){        String dateOfBegin = "2018-09-28";        System.out.println(getMonthOfThis(dateOfBegin));       /* String today = DateUtil.formatDate(new Date());        System.out.println(DateUtil.compareDate(today,dateOfBegin));        System.out.println(getlastDay(formatDate(new Date())));        System.out.println(getlastMonth(formatDate(new Date())));*/    }    /**     * 获得指定日期的前一周     * @param date     * @return     */    public static String getlastWeek(String date){        Calendar c = Calendar.getInstance();        Date date_ = parseDate(date);        c.setTime(date_);        int day=c.get(Calendar.DATE);        c.set(Calendar.DATE,day-7);        return formatDate(c.getTime());    }}

 

转载于:https://www.cnblogs.com/mytripod/p/9733441.html

你可能感兴趣的文章
Java线程面试题
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
Java大数——a^b + b^a
查看>>
【动态规划】skiing
查看>>
java定时器的使用(Timer)
查看>>
boost 同步定时器
查看>>
[ROS] Chinese MOOC || Chapter-4.4 Action
查看>>
简单的数据库操作
查看>>
Redmine
查看>>
帧的最小长度 CSMA/CD
查看>>
编程算法 - 左旋转字符串 代码(C)
查看>>
Python3多线程爬取meizitu的图片
查看>>
树状数组及其他特别简单的扩展
查看>>
110104_LC-Display(液晶显示屏)
查看>>
php学习笔记
查看>>