简单高效的JAVA日期处理工具类 -- 日期时间加减,大小比较

本文介绍了一个Java项目中使用的日期时间操作工具类,包括日期格式化、解析、比较、加减等常用功能,利用Joda-Time库进行高效且灵活的日期时间处理。
package com.game.admin.util;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static final String DATE_FORMATE_LONG = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_FORMATE_SHOT = "yyyy-MM-dd";

	public static final String DATE_FORMAT_8 = "yyyyMMdd";

	public static final String TIME_FORMATE_SHOT = "HH:mm:ss";

	public static final String TIME_FORMAT_8 = "yyMMdd";

    public static final Integer SECOND_IN_24_H = 86400;


	public static boolean compareTo(Integer minute, Date dateNew, Date dateOld){
		Calendar calendarNew = Calendar.getInstance();
		Calendar calendar = Calendar.getInstance();
		calendarNew.setTime(dateNew);
		calendar.setTime(dateOld);
		calendarNew.add(Calendar.MINUTE, -1 * minute);
		return calendar.compareTo(calendarNew) != -1 ? false : true;
	}
	public static Date getCurrentDate(){
		DateTime dateTime = DateTime.now();
		return dateTime.toDate();
	}

	public static String getDateShort(){
		DateTime dateTime = DateTime.now();
		return formatDate(dateTime.toDate(),TIME_FORMAT_8);
	}

	public static String getCurDateString() {
		DateTime dateTime = DateTime.now();
		return formatDate(dateTime.toDate(),DATE_FORMATE_SHOT);
	}

	public static String getCurDate() {
		DateTime dateTime = DateTime.now();
		return formatDate(dateTime.toDate(),DATE_FORMATE_LONG);
	}

	public static Date parseDate(String dateStr){
		return parseDate(dateStr, DATE_FORMATE_LONG);
	}

	public static Date parseDate(String dateStr,String dateFormat) {
		DateTimeFormatter format = DateTimeFormat.forPattern(dateFormat);
		DateTime dateTime = format.parseDateTime(dateStr);
		return dateTime.toDate();
	}

	public static String formatDate(Date date) {
		return formatDate(date,DATE_FORMATE_LONG);
	}

	public static String formatDate(Date date,String dateFormat) {
		DateTime dateTime = new DateTime(date);
		return dateTime.toString(dateFormat);
	}

	public static Date addMiniter(Date date,int mins) {
		DateTime dateTime = new DateTime(date);
		DateTime d = dateTime.plusMinutes(mins);
		return d.toDate();
	}

    public static Date addSecond(Date date,int second) {
        DateTime dateTime = new DateTime(date);
        DateTime d = dateTime.plusSeconds(second);
        return d.toDate();
    }

    public static Date addDays(Date date,int days){
        DateTime dateTime = new DateTime(date);
        DateTime d = dateTime.plusDays(days);
        return d.toDate();
    }
    public static Date addYears(Date date,int years){
        DateTime dateTime = new DateTime(date);
        DateTime d = dateTime.plusYears(years);
        return d.toDate();
    }

	public static String getTime(Date date) {
		DateTime dateTime = new DateTime(date);
		String time = dateTime.toString(TIME_FORMATE_SHOT);
		return time;
	}

	public static boolean isBetweenTime(String startTime,String endTime,String time) {
		DateTimeFormatter format = DateTimeFormat.forPattern(TIME_FORMATE_SHOT);
		DateTime dt1 = format.parseDateTime(startTime);
		DateTime dt2 = format.parseDateTime(endTime);
		DateTime dt = format.parseDateTime(time);
		if(dt.isAfter(dt1) && dt.isBefore(dt2)) {
			return true;
		}else {
			return false;
		}
	}

    public static boolean isBetween(Long startTime, Long endTime, String time) throws ParseException {
        Long times = getTimes(time);
        return times>startTime && times<endTime;
    }

    public static Long getTimes(String s) throws ParseException {
        SimpleDateFormat sdformat = new SimpleDateFormat(TIME_FORMATE_SHOT);
        Date date = sdformat.parse(s);
        return date.getTime();
    }

    public static Integer datePassedSeconds(Date date) {
        if(date == null) {
            return 0;
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);

        return hour * 3600 + minute * 60 + second;
    }


	public static void main(String[] args){
		System.out.println(parseDate("2018-11-11 22:11:00"));
	}

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

化猿和尚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值