java 时间格式化工具类_Java处理Date时间格式的各种场景工具类(一)

public class DateUtil {

public enum DateType {

YEAR, MONTH, DAY, HH, MI, SS, YYYY_MM_DD, YYYYMMDD

}

public static java.sql.Date maxDate() {

return java.sql.Date.valueOf("9999-09-09");

}

/**

* 得到当前应用服务器的系统日期

*

* @return 系统日期

*/

public static Timestamp sysTimestamp() {

return new Timestamp(System.currentTimeMillis());

}

/**

* 得到当前应用服务器的系统日期

*

* @return 系统日期

*/

public static java.sql.Date sysDate() {

return new java.sql.Date(System.currentTimeMillis());

}

/**

* 得到当前应用服务器的系统时间

*

* @return 系统日期

*/

public static java.sql.Time sysTime() {

return new java.sql.Time(System.currentTimeMillis());

}

/**

* 得到当前应用服务器的系统日期

* 字符串类型

*

* @return 系统日期

*/

public static String sysDate4Str() {

return new java.sql.Date(System.currentTimeMillis()).toString();

}

/**

* 得到d1与d2之间相差数值

*

* 数值可以为年份,DateType.YEAR

* 数值可以为月份,DateType.MONTH

* 数值可以为天数,DateType.DAY

* 数值可以为小时,DateType.HH

* 数值可以为分钟,DateType.MI

* 数值可以为秒,DateType.SS

*

* @param d1

* 日期1(较大的日期)

* @param d2

* 日期2(较小的日期)

* @param dataType

* 数值类型

* @return 相差数值

* d1早于d2时,返回-1。

* dateType类型不正确时,返回-2。

*/

public static int dateBetween(Date d1, Date d2, DateType dateType) {

if(d1.equals(d2)){

return 0;

}

if (!d2.before(d1)) {

return -1;

}

Calendar c1 = Calendar.getInstance();

c1.setTime(d1);

Calendar c2 = Calendar.getInstance();

c2.setTime(d2);

if (DateType.YEAR.equals(dateType)) {

return c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);

}

if (DateType.MONTH.equals(dateType)) {

return (c1.get(Calendar.YEAR)

- c2.get(Calendar.YEAR)) * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);

}

if (DateType.DAY.equals(dateType)) {

return (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 86400000L);

}

if (DateType.HH.equals(dateType)) {

return (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 3600000L);

}

if (DateType.MI.equals(dateType)) {

return (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 60000L);

}

if (DateType.SS.equals(dateType)) {

return (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / 1000L);

}

return -2;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值