java Date 处理常用方法

    public static final String SDF1 = "yyyy-MM-dd HH:mm:ss";
    public static final String SDF2 = "yyyy-MM-dd";
    public static final String SDF3 = "yyyyMMdd";
    public static final String SDF4 = "yyyy-MM";
    public static final String SDF5 = "yyyy";
    public static final String SDF6 = "yyMM";

/**
     * 获取时间增加n天后的时间,s 时间格式
     * @param date 
     *             时间
     * @param s 
     *             时间格式
     * @param n 
     *             数量
     * @return
     */
    public static String getDateAddDayOfter(String date,String s,int n){
        try{
            Date date2 =getDate(s,date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date2);
            calendar.add(Calendar.MINUTE, n*24*60);
            SimpleDateFormat sdf=new SimpleDateFormat(s);
            String dateStr=sdf.format(calendar.getTimeInMillis());
            return dateStr;
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

/**
     * 根据特定格式获取当前时间+n天后的时间
     * @param s 
     *         数据返回格式
     * @param n 
     *         天数
     * @return
     */
    public static Date getDateAddDayOfter(String s,int n){
        try{
            Calendar now=Calendar.getInstance();
            now.add(Calendar.MINUTE,n*24*60);
            SimpleDateFormat sdf=new SimpleDateFormat(s);
            String dateStr=sdf.format(now.getTimeInMillis());
            Date currentTime_2 = sdf.parse(dateStr);
            return currentTime_2;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

/**
     * 根据特定格式获取当前时间+n个小时后的时间
     * @param s 数据返回格式
     * @param n 小时数
     * @return
     */
    public static Date getDateAddHourAfter(String s,int n){
        try{
            Calendar now=Calendar.getInstance();
            now.add(Calendar.MINUTE,n*60);
            SimpleDateFormat sdf=new SimpleDateFormat(s);
            String dateStr=sdf.format(now.getTimeInMillis());
            Date currentTime_2 = sdf.parse(dateStr);
            return currentTime_2;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

 

/**
     * 根据特定格式获取当前时间date
     * @throws ParseException 
     */
    public static Date getNowDate(String s){
        try{
             Date currentTime = new Date();
             SimpleDateFormat df = new SimpleDateFormat(s);//设置日期格式
             String dateString = df.format(currentTime);
             Date currentTime_2 = df.parse(dateString);
             return currentTime_2;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
         
    }

 

/**
     * 获取时间字符串形式
     * @param s
     *         格式
     * @param date
     *             时间
     * @return
     */
    public static String getDate(String s,Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(s);
        return sdf.format(date);
    }

/**
     * 获取字符串时间的Date
     * @param s
     *             格式
     * @param date
     *             时间
     * @return
     */
    public static Date getDate(String s,String date){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(s);
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取到秒的时间戳
     */
    public static String getSecondTimestamp(){
        return getTimeMillByBit(10);
    }
    /**
     * 根据特定格式获取当前时间字符串
     */
    public static String getNowDateString(String s ){
        SimpleDateFormat df = new SimpleDateFormat(s);//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
    }
    /**
     * 获取特定位数的时间戳
     */
    public static String getTimeMillByBit(int bit){
        if(bit>=13){
            return getTimeMillis();
        }else{
            String timeM = getTimeMillis();
            return timeM.substring(timeM.length()-bit, timeM.length());
        }
    }

/**
     * 获取当前时间时间戳
     * @return
     */
    public static  String getTimeMillis(){
        return String.valueOf(new Date().getTime());
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值