Java日期工具类


Java日期工具,转化成类型:

   public final static String YYYY_MM_DD_HH_MM    = "yyyy-MM-dd HH:mm";
    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYY_MM_DD_HH       = "yyyy-MM-dd HH";
    public final static String YYYY_MM_DD          = "yyyy-MM-dd";
    public final static String YYYY_MM             = "yyyy-MM";
    public final static String YYYYMMDD            = "yyyyMMdd";
    public final static String YYYYMMDDHH          = "yyyyMMddHH";
    public final static String YYYYMMDDHHMMSS      = "yyyyMMddHHmmss";
    public final static String YYYYMMDDHHMM        = "yyyyMMddHHmm";

具体类:

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


import org.apache.commons.lang.StringUtils;




public class DateUtil {


    public final static String YYYY_MM_DD_HH_MM    = "yyyy-MM-dd HH:mm";
    public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public final static String YYYY_MM_DD_HH       = "yyyy-MM-dd HH";
    public final static String YYYY_MM_DD          = "yyyy-MM-dd";
    public final static String YYYY_MM             = "yyyy-MM";
    public final static String YYYYMMDD            = "yyyyMMdd";
    public final static String YYYYMMDDHH          = "yyyyMMddHH";
    public final static String YYYYMMDDHHMMSS      = "yyyyMMddHHmmss";
    public final static String YYYYMMDDHHMM        = "yyyyMMddHHmm";


    /**
     * 通过制定的格式,将日期字符串解析为java.util.Date对象
     * 
     * @param dateStr 日期字符串
     * @param formatStr 解析的格式
     * @return 转换后的结果:Date对象
     * @throws ParseException
     */
    public static Date strToDate(String dateStr, String formatStr) {
        Date date = null;
        if (dateStr != null && !"".equals(dateStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
            try {
                date = sdf.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }


    /**
     * 根据一个完整的日期,格式化成一个只到分钟的时间.yyyy-MM-dd HH:mm
     * 
     * @param date
     * @param formatFormat
     * @param toFormat
     * @return
     */
    public static Date getMinuteDate(String date, String formatFormat) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(strToDate(date, formatFormat));
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }


    /**
     * 根据一个完整的日期,格式化成一个只到天的时间.yyyy-MM-dd
     * 
     * @param date
     * @param formatFormat
     * @return
     */
    public static Date getDayDate(String date, String formatFormat) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(strToDate(date, formatFormat));
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }


    /**
     * 通过制定的格式,将Date对象格式化为字符串
     * 
     * @param date 需要转换的Date对象
     * @param formatStr 转换的格式
     * @return 转换之后的字符串
     */
    public static String dateToStr(Date date, String formatStr) {
        String result = null;
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
            result = sdf.format(date);
        }
        return result;
    }


    /**
     * 将字符串,格式化.
     * 
     * @param date
     * @param format
     * @return
     */
    public static String parseDateStr(String date, String format) {
        if (StringUtils.isEmpty(date) || StringUtils.isEmpty(format)) {
            return null;
        }
        SimpleDateFormat f = new SimpleDateFormat(format);


        return f.format(strToDate(date, format));
    }


    /**
     * 将一个字符串日期转换成另一个字符串日期.
     * 
     * @param date 日期对象
     * @param fromFormat 原来的日期格式
     * @param toFormat 转换后的日期格式
     * @return
     */
    public static String parseDateStr(String date, String fromFormat, String toFormat) {
        return dateToStr(strToDate(date, fromFormat), toFormat);
    }


    //
    /**
     * 获取当前系统时间
     * 
     * @return
     */
    public static Date getCurrDate() {
        return new Date();
    }


    /**
     * 获取当前指定格式 的系统时间,可以改变分钟的数值 ,加或者减,例如:-1,10.
     * 
     * @param formatStr 日期的格式化.
     * @param changeMinute 正数或者负数。
     * @return
     */
    public static String getCurrCustomMinuteDate(String formatStr, int changeMinute) {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getCurrDate());
        cal.add(Calendar.MINUTE, changeMinute);
        return dateToStr(cal.getTime(), formatStr);
    }


    /**
     * 获取当前指定格式的系统时间
     * 
     * @param formatStr
     * @return
     */
    public static String getCurrDate(String formatStr) {
        return dateToStr(getCurrDate(), formatStr);
    }


    /**
     * 获取指定日期的小时 如果date为nul则返回-1.
     * 
     * @param date
     * @return
     */
    public static int getHour(Date date) {
        if (date == null) {
            return -1;
        }
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY);
    }


    /**
     * 获取指定日期的天,如果date为null则返回-1;
     * 
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        if (date == null) {
            return -1;
        }
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 获取当月最大的天数.
     * 
     * @param date
     * @return
     */
    public static int getMonthMaxDay(Date date) {
        if (date == null) {
            return -1;
        }
        Calendar cal = new GregorianCalendar();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DATE);
    }


    /**
     * 获取当前日期对应的上一个月的日期.
     * 
     * @return
     */
    public static Date getCurrUpMonthDate() {
        Calendar c = new GregorianCalendar();
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        return c.getTime();
    }


    /**
     * 获取当前日期对应的上一天的日期.
     * 
     * @return
     */
    public static Date getCurrUpDayDate() {
        Calendar c = new GregorianCalendar();
        c.setTime(new Date());
        c.add(Calendar.DATE, -1);
        return c.getTime();
    }


    public static void main(String[] args) {


        String s = dateToStr(getCurrUpMonthDate(), YYYY_MM_DD_HH_MM_SS);
        System.out.println(s);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值