import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

public class DateUtil {
    public static final String DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE = "yyyy-MM-dd";

public DateUtil() {
    }

public static String datetimeToStr(Date date, String format) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

public static String dateTimeToStr(Date date) {
        return datetimeToStr(date, "yyyy-MM-dd HH:mm:ss");
    }

public static String dateToStr(Date date) {
        return datetimeToStr(date, "yyyy-MM-dd");
    }

public static String dateToStr(Date date, String format) {
        return datetimeToStr(date, format);
    }

public static String getCurrentDate() {
        return (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
    }

public static String getCurrentDate(String format) {
        return (new SimpleDateFormat(format)).format(new Date());
    }

public static String getCurrentDatetime() {
        return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
    }

public static String getCurrentDatetime(String format) {
        return (new SimpleDateFormat(format)).format(new Date());
    }

public static int getCurrentTimeHashCode() {
        return String.valueOf(System.currentTimeMillis()).hashCode();
    }

public static Date getDateBegin(Date date) {
        SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (date != null) {
            try {
                return DateFormat.getDateInstance(2, Locale.CHINA).parse(ymdFormat.format(date));
            } catch (ParseException var3) {
                System.err.println("DataFromat error");
            }
        }

return null;
    }

public static Date getDateEnd(Date date) {
        SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (date != null) {
            try {
                Date endDate = strToDate(ymdFormat.format(new Date(date.getTime() + 86400000L)));
                endDate = new Date(endDate.getTime() - 1000L);
                return endDate;
            } catch (Exception var3) {
                System.err.println("DataFromat error");
            }
        }

return null;
    }

public static long getNow() {
        return System.currentTimeMillis();
    }

public static String getTime() {
        Date d = new Date();
        String re = datetimeToStr(d, "yyyyMMddHHmmssSSS");
        return re;
    }

public static String getTime(String format) {
        Date d = new Date();
        String re = datetimeToStr(d, format);
        return re;
    }

public static Date strToFormatDate(String date, String format) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date, new ParsePosition(0));
        }
    }

public static Date strToDate(String date) {
        return strToFormatDate(date, "yyyy-MM-dd");
    }

public static final Date strToDate(String dateStr, String format) {
        return strToFormatDate(dateStr, format);
    }

public static Date strToDateTime(String date) {
        return strToFormatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

public static Date strToDateTime(String date, String format) {
        return strToFormatDate(date, format);
    }

public static Timestamp strToTimestamp(String str) throws Exception {
        if (StringUtils.isEmpty(str)) {
            throw new Exception("转换错误");
        } else {
            return str.trim().length() > 10 ? new Timestamp((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(str).getTime()) : new Timestamp((new SimpleDateFormat("yyyy-MM-dd")).parse(str).getTime());
        }
    }

public static Timestamp strToTimestamp(String sDate, String sFormat) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(sFormat);
        Date t = sdf.parse(sDate);
        return new Timestamp(t.getTime());
    }

public static boolean validateExpireDate(long timeMillis, long expireTimeMillis) {
        return getNow() - timeMillis > expireTimeMillis;
    }

public static String getHHmmssSSS() {
        Date d = new Date();
        return getHHmmssSSS(d);
    }

public static String getHHmmssSSS(Date d) {
        int hh = Integer.valueOf(datetimeToStr(d, "HH"));
        int mm = Integer.valueOf(datetimeToStr(d, "mm"));
        int ss = Integer.valueOf(datetimeToStr(d, "ss"));
        int sss = Integer.valueOf(datetimeToStr(d, "SSS"));
        int time = 0;
        if (hh != 0) {
            time += hh * 60 * 60 * 1000;
        }

if (mm != 0) {
            time += mm * 60 * 1000;
        }

if (ss != 0) {
            time += ss * 1000;
        }

if (sss != 0) {
            time += sss;
        }

String str;
        for(str = String.valueOf(time); str.length() < 8; str = "0" + str) {
            ;
        }

return str;
    }

public static Date caculateDate(Date date, Integer count, int status) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(status, calendar.get(status) + count);
        return calendar.getTime();
    }

public static int caculateDaysNumber(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, month - 1);
        return cal.getActualMaximum(5);
    }

public static Date caculateDateByNextBaseMonth(Date now, Integer month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.set(2, cal.get(2) + month + NumberUtils.INTEGER_ONE);
        cal.set(5, NumberUtils.INTEGER_ZERO);
        return cal.getTime();
    }

public static Date caculateFinallyDatebyDateType(Date date, int dateType, int dateNumber) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch(dateType) {
        case 1:
            calendar.add(dateType, dateNumber + 1);
            calendar.set(2, 0);
            calendar.set(5, NumberUtils.INTEGER_ZERO);
            break;
        case 2:
            calendar.add(2, dateNumber + 1);
            calendar.set(5, NumberUtils.INTEGER_ZERO);
            break;
        case 3:
        case 4:
        default:
            throw new RuntimeException("dateType value error");
        case 5:
            calendar.add(5, dateNumber);
        }

return calendar.getTime();
    }

public static int compareTime(String dateTime1, String dateTime2) {
        if (!StringUtils.isEmpty(dateTime1) && !StringUtils.isEmpty(dateTime1)) {
            DateFormat df = null;
            if (dateTime1.length() == 10 && dateTime2.length() == 10) {
                df = new SimpleDateFormat("yyyy-MM-dd");
            } else {
                if (dateTime1.length() != 19 || dateTime2.length() != 19) {
                    return 2;
                }

df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }

Calendar c1 = Calendar.getInstance();
            Calendar c2 = Calendar.getInstance();

try {
                c1.setTime(df.parse(dateTime1));
                c2.setTime(df.parse(dateTime2));
            } catch (ParseException var6) {
                System.err.println("格式不正确");
            }

int result = c1.compareTo(c2);
            return result;
        } else {
            return 2;
        }
    }

public static void main(String[] args) {
        Date now = new Date();
        String nowStr = dateTimeToStr(now);
        System.out.println(nowStr);
        Date fDate = caculateFinallyDatebyDateType(now, 10, 3);
        System.out.println(dateTimeToStr(fDate));
    }
}

java时间操作工具类的更多相关文章

  1. java/javascript 时间操作工具类

    一.java 时间操作工具类 import org.springframework.util.StringUtils; import java.text.ParseException; import ...

  2. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  3. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  4. JavaScript时间操作工具类

    /** * 时间操作工具类 * * @author zwq * */ var TimeFrameUtil = { /** * 格式化日期 * @param date {Date} 日期 * @para ...

  5. docker 部署vsftpd服务、验证及java ftp操作工具类

    docker部署vsftpd服务 新建ftp文件存储目录/home/ftp cd /home mkdir ftp 创建一个组,用于存放ftp用户 groupadd ftpgroups 创建ftp用户, ...

  6. Java时间处理类LocalDate和LocalDateTime常用方法

    Java时间处理类LocalDate和LocalDateTime常用方法 https://blog.csdn.net/weixin_42579074/article/details/93721757

  7. Java时间转换类实现

    Java时间类型非常的差,首先版本问题,本人使用java在Android平台开发.很多Data类的方法已经不提倡使用,一个时间,居然要使用Calendar.DateFormat等类共同编码,非常麻烦. ...

  8. 超详细的Java时间工具类

    package com.td.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.Pa ...

  9. JAVA文件操作工具类(读、增、删除、复制)

    使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...

随机推荐

  1. (转)Systemd 入门教程:命令篇

    Systemd 入门教程:命令篇 原文:http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html Systemd 入门 ...

  2. Git学习系列之 Git 、CVS、SVN的比较

    Git .CVS.SVN比较 项目源代码的版本管理工具中,比较常用的主要有:CVS.SVN.Git 和 Mercurial  (其中,关于SVN,请参见我的博客:SVN学习系列) 目前Google C ...

  3. CSS ::Selection的使用方法

    大家都知道浏览器对选中的文本默认样式都是统一的,Windows下是一个深蓝色的背景,白字的前景,而在Mac下是一个淡蓝色背景,白色字体,就如上图所展示的一样,自从有了这个“::selection”选择 ...

  4. idea tomcat添加

    https://blog.csdn.net/fanhenghui/article/details/69258499

  5. Game-Tech小游戏专场第二趴,这次帝都见

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云 发表于云+社区专栏 自从小游戏推出以来,凭借微信带来的巨大流量和变现能力,小游戏生态极速地建立了起来,短短半年多时间已经出 ...

  6. TCP/IP协议簇分层详解---转

    http://blog.csdn.net/hankscpp/article/details/8611229 一. TCP/IP 和 ISO/OSI ISO/OSI模型,即开放式通信系统互联参考模型(O ...

  7. hexo+github搭建博客跳坑

    hexo+GitHub搭建博客过程中,hexo安装成功,可以启动和运行,但是访问localhost:4000却无法访问,弄了半天,最后发现是福昕阅读器占用了4000端口 解决办法: 采用命令hexo ...

  8. PHP学习3——数组

    主要内容: 简介 常用的方法 循环遍历数组 PHP预定义数组 数组的处理函数 数组 PHP由于是弱类型的语言,他的变量类型是可以自由变换的,他的数组很自由,长度是可以动态增加的. 他的索引默认为数字0 ...

  9. [转]what’s the difference between @Component ,@Repository & @Service annotations in Spring

    原文地址:https://www.cnblogs.com/softidea/p/6070314.html @Component is equivalent to <bean> @Servi ...

  10. vs2017启动调试,点击浏览器或输入后回车浏览器闪退,调试中断

    vs2017在启动调试后,浏览器运行,点击地址栏刚输入几个字符,mmmmm居然闪退了! 什么情况呢?测试一下,换其他浏览器进行调试,偶尔不会有问题, 可是第二天......还是一下 于是浏览器——ww ...