Java时间工具类


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

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;

/**
 * @author 
 * @version 1.0
 */
public class DateUtil {

    public static final String TIME_IN_MILLIS = "HHmmssSSS";
    public static final String TIME_MMDDHHMMSS = "MMddHHmmss";
    public static final String TIME_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String TIME_YYMMDDHHMMSS = "YYMMddHHmmss";
    public static final String DATE_STR = "yyMMdd";
    public static final String DATE_TIME_STR = "HHmmss";
    public static final String DATE_YYYYMMDD = "yyyyMMdd";
    /**
     * 格式化时间格式
     */
    public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_IN_ALL = "yyyyMMddHHmmssSSS";
    /**
     * 系统日期,auth_tech_parameter中系统日期配置的main_key;
     */
    private static final String SYS_DATE = "SYS_DATE";
    /**
     * 系统日期偏移量,auth_tech_parameter中系统日期配置的sub_key;
     */
    private static final String OFFSET = "OFFSET";

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);
    /**
     * 上次查询日期
     */
    private static volatile String lastDate;

    /**
     * 获取当前时间(hhmmssSSS)
     *
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_IN_MILLIS);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    /**
     * 获取当前时间(MMddhhmmss)
     *
     * @return
     */
    public static String getTransDateTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TIME_MMDDHHMMSS);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    public static String getDateStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_STR);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    public static String getTimeStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_TIME_STR);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }
    public static String get13TimeStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FULL_STR);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    /**
     * 获取yyyyMMdd格式当前日期,并转换为String字符串
     * @return
     */
    public static String getCurrentDateStr() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_YYYYMMDD);
        Date current = new Date();
        return simpleDateFormat.format(current);
    }

    /**
     * 判断日期是否为有效日期
     * @param strDate 日期字符串
     * @return
     */
    public static Boolean parse(String strDate, String formatType) {
        if (StringUtils.isBlank(strDate)) {
            return false;
        }
        SimpleDateFormat df = new SimpleDateFormat(formatType);
        try {
            df.setLenient(false);
            df.parse(strDate);
            return true;
        } catch (ParseException e) {
            logger.error("日期格式转换错误 dateStr:" + strDate + "format:" + formatType, e);
            return false;
        }
    }

    /**
     * 日期格式转换,将日期转换为指定格式字符串
     * @param date
     * @param type
     * @return
     */
    public static String getDateByType(Date date, String type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        return simpleDateFormat.format(date);
    }

    public static String parseToString(Date strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(strDate);
    }

    public static int compareDate(String date1, String date2) {
        Preconditions.checkArgument(StringUtils.isNoneBlank(date1), "输入参数date1不能为空");
        Preconditions.checkArgument(StringUtils.isNoneBlank(date2), "输入参数date2不能为空");
        Preconditions.checkArgument(date1.length() == date2.length(), "日期格式必须一致");
        return compareDate(Long.parseLong(date1), Long.parseLong(date2));
    }

    public static int compareDate(long date1, long date2) {
        if (date1 > date2) {
            return 1;
        } else if (date1 < date2) {
            return -1;
        }
        return 0;
    }

    /**
     * 格式化字符串日期(Fri May 18 15:39:46 CST 2018)
     * @param strDate 日期
     * @return
     */
    public static String format(Date strDate) {
        if (strDate == null) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FULL_STR, Locale.ENGLISH);
        return dateFormat.format(strDate);
    }

    /**
     * 日期加减
     * @param src    起始日期
     * @param type   日期类型(使用Calendar里面的常量)
     * @param amount 数量
     * @return
     */
    public static Date addDate(Date src, int type, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.add(type, amount);
        return calendar.getTime();
    }

    /**
     * 使用用户格式提取字符串日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期格式
     * @return
     */
    /*public static Date parseDate(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (Exception e) {
            throw new MngException(BizErrorMsg.SYSTEM_ERROR, e, "日期格式不正确");
        }
    }*/

    /**
     * 获取yddd格式Julian date
     * @return
     */
    public static String getJulianDate() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("Europe/London"));
        return String.valueOf(calendar.get(Calendar.YEAR) % 10)
                + StringUtils.leftPad(String.valueOf(calendar.get(Calendar.DAY_OF_YEAR)), 3, '0')
                + StringUtils.leftPad(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)), 2, '0');
    }

    /**
     * 获取伦敦当前时间(GMT时间)
     * @return
     */
    public static String getLondonCurrentDateTime() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_YYYYMMDDHHMMSS);
        dateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London"));
        return dateFormat.format(new Date());
    }


    /**
     * String转Date类型
     *
     * @param datestr 日期字符串
     * @return
     */
    public static Date stringToDate(String datestr) {

        if (datestr == null || datestr.equals("")) {
            return null;
        }
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(DateUtil.DATE_STR);
        try {
            date = df.parse(datestr);
        } catch (ParseException e) {
            date = DateUtil.stringToDate(datestr, "yyyyMMdd");
        }
        return date;
    }


    /**
     * String转Date类型
     *
     * @param datestr 日期字符串
     * @param dateformat 日期格式
     * @return
     */
    public static Date stringToDate(String datestr, String dateformat) {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(dateformat);
        try {
            date = df.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    /** 
     * 时间戳转换成日期格式字符串 
     * @param seconds 精确到秒的字符串 
     * @param formatStr 
     * @return 
     */  
    public static String timeStamp2Date(String seconds,String format) {  
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){  
            return "";  
        }  
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }   
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(new Date(Long.valueOf(seconds+"000")));  

    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值