时间操作工具类

public class DateUtils {
  public static int diffDay(String d1,String d2,String patten){
      SimpleDateFormat format=new SimpleDateFormat(patten);
      Date date1=null;
      Date date2=null;
      int day=0;
      try{
          date1=format.parse(d1);
          date2=format.parse(d2);
         
          
          long m=Math.abs(date1.getTime()-date2.getTime());
          day=(int)(m/1000/60/60/24);
          
      }catch(ParseException e){
          e.printStackTrace();
      }
      return day;
  }  
    public static Date getdate(long m){
        return new Date(m);
    }    
    public static Date getcurrentdate(){
        return new Date();
    }
    public static Date getchangedate(boolean isplus,int day){
        //获得当前时间的时间戳(自1970年到)
        long currenttime=System.currentTimeMillis();
        long changetime=0;
        if(isplus){
            changetime=currenttime+(day*24*60*60*1000);
        }else{
            changetime=currenttime-(day*24*60*60*1000);
        }
        return new Date(changetime) ;
    }
    public static Date getchangetime(boolean isplus,int day,Date date){
        long time=date.getTime();
        long changeTime=0;
        if(isplus) {
            //添加日期
            changeTime=time+(day*24*60*60*1000);
        } else {
            //减少日期
            changeTime=time-(day*24*60*60*1000);
        }
        return new Date(changeTime);
    }
    public static String getformatdate(Date date,String format){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }
    public static long getCurrentMillis(){
        return System.currentTimeMillis();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
日期操作工具类 /** * 将日期对象按照换为时间字符串 * 默认格式为"yyyy-MM-dd HH:mm:ss" * @param sourceTime * @return * @throws ParseException */ public static String formatDate(Date sourceDate) /** * 将日期对象按照指定格式转换为时间字符串 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static String formatDate(Date sourceDate, String formatStr) /** * 将时间字符串按照指定格式转换为时间字符串 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static String formatDate(String sourceTime, String formatStr) /** * 得到当前时间指定天数之前几天的日期 * @param days 指定的天数 * @return */ public static Date getFormerDateOfDays(int days) /** * 得到当前时间之前几个小时的日期 * @param hours 指定的小时数 * @return */ public static Date getFormerDateOfHours(int hours){} /** * 得到当前时间之前多少秒的日期 * @param sec 指定的秒数 * @return */ public static Date getFormerDateOfSecond(int sec){ } /** * 得到指定日期之前sec秒的日期 * @param days 指定的天数 * @return */ public static Date getFormerDateOfSecond(Date date, int sec){ } /** * 将时间字符串转换为日起对象 * 默认格式为“yyyy-MM-dd HH:mm:ss” * “yyyy/MM/dd HH:mm:ss”也可以 * @param sourceTime * @return * @throws ParseException */ public static Date paresDate(String sourceTime) throws ParseException { } /** * 返回指定月份的最后一天 * @param year * @param month * @return */ public static int getLastDayOfMonth(int year, int month){ } /** * 计算两个日期间的秒数 * @param beginDate * @param endDate * @return * @throws ParseException */ public static long getSecondBetweenDate(String beginDate, String endDate) throws ParseException { } /** * 将时间字符串按照指定格式转换为日期对象 * @param sourceTime * @param formatStr * @return * @throws ParseException */ public static Date paresDate(String sourceTime, String formatStr) throws ParseException { }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值