对时间的封装util

/**
 * 将一个秒数的时间转成形如00:00:00字符串
 */
public static String parseTime(String date) {
    int time = Integer.parseInt(date);
    int m = time / 60 % 60;
    int s = time % 60;
    String mm = m < 10 ? "0" + m : "" + m;
    String ss = s < 10 ? "0" + s : "" + s;
    return "00:" + mm + ":" + ss;

}

/**获取到日期  比如前三天显示  今天/昨天/前天
    超过三天前且在一年内的显示日期如:10-20
   超过一年的显示年月日如:2014-10-25*/
public static String getDateByDisparity(long seconds) { String date = ""; //秒 long disparity = System.currentTimeMillis() / 1000; //当前的年月日 String curYear = getYearDate(disparity); String curMouth = getMonthDate2(disparity); String curDay = getDayDate(disparity); //返回数据的年月日 String year = getYearDate(seconds); String mouth = getMonthDate2(seconds); String day = getDayDate(seconds); LogUtil.d("curYear=" + curYear + "curMouth= " + curMouth + "curDay" + curDay + "year=" + year + "mouth=" + mouth + "day=" + day); if (curYear.equals(year)) { if (curMouth.equals(mouth)) { if (curDay.equals(day)) { date = "今天"; } else { if (Integer.parseInt(curDay) - Integer.parseInt(day) == 1) { date = "昨天"; } else if (Integer.parseInt(curDay) - Integer.parseInt(day) == 2) { date = "前天"; } else { date = getMDDate(seconds); } } } else { date = getMDDate(seconds); } } else { date = getYMDSDateLine(seconds); } return date;}
/**  获取到日期  前三天显示  今天 10:20 /昨天 10:20 /前天10:20
       超过三天前且在一年内的显示日期如:10-20 11:20
      超过一年的显示年月日如:2014-10-25*/
public static String getDateByDisparity2(long seconds) {
    String date = "";
    //秒
    long disparity = System.currentTimeMillis() / 1000;
    //当前的年月日
    String curYear = getYearDate(disparity);
    String curMouth = getMonthDate2(disparity);
    String curDay = getDayDate(disparity);
    //返回数据的年月日
    String year = getYearDate(seconds);
    String mouth = getMonthDate2(seconds);
    String day = getDayDate(seconds);
    LogUtil.d("curYear=" + curYear + "curMouth= " + curMouth + "curDay" + curDay + "year=" + year + "mouth=" + mouth + "day=" + day);
    if (curYear.equals(year)) {
        if (curMouth.equals(mouth)) {
            if (curDay.equals(day)) {
                date = "今天" + getHSDate(seconds);
            } else {
                if (Integer.parseInt(curDay) - Integer.parseInt(day) == 1) {
                    date = "昨天" + getHSDate(seconds);
                } else if (Integer.parseInt(curDay) - Integer.parseInt(day) == 2) {
                    date = "前天" + getHSDate(seconds);
                } else {
                    date = getMDHSDate(seconds);
                }
            }
        } else {
            date = getMDHSDate(seconds);
        }
    } else {
        date = getYMDSDateLine(seconds);
    }
    return date;
}
/**
 * seconds为秒
 **/
public static String getHSDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
    return dateFormat.format(new Date(millisecond));
}


public static String getYearDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
    return dateFormat.format(new Date(millisecond));
}

public static String getMonthDate2(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM");
    return dateFormat.format(new Date(millisecond));
}

public static String getDayDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
    return dateFormat.format(new Date(millisecond));
}

public static String getMonthDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("M");
    return dateFormat.format(new Date(millisecond));
}

public static String getMDDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
    return dateFormat.format(new Date(millisecond));
}

public static String getMDHSDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
    return dateFormat.format(new Date(millisecond));
}

public static String getYMDSDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    return dateFormat.format(new Date(millisecond));
}

public static String getYMDSDatePoint(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
    return dateFormat.format(new Date(millisecond));
}

public static String getYMDSDateLine(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return dateFormat.format(new Date(millisecond));
}

public static String getYMDSDHSateLine(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return dateFormat.format(new Date(millisecond));
}

public static String getMDSDate(long seconds) {
    long millisecond = seconds * 1000;
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
    return dateFormat.format(new Date(millisecond));
}

public static String getMSDate(long seconds) {
    long millisecond = seconds;
    SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
    return dateFormat.format(new Date(millisecond));
}

public static String getYMDSDateLine2(long seconds) {
    long millisecond = seconds;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return dateFormat.format(new Date(millisecond));
}


public static String getMonthDayDate(long seconds) {

    int millisecond = (int) seconds / (3600 * 24);
    if (millisecond > 30) {
        return (millisecond / 30) + "个月";
    }
    return millisecond + "天";
}
/**
 * 比较时间差
 *
 * @param time
 * @return
 */
@SuppressLint("SimpleDateFormat")
public static boolean isExpire(String time) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date date = format.parse(time);
        long overtime = date.getTime();
        long currenttime = System.currentTimeMillis();
        int expire = (int) ((overtime - currenttime) / (10 * 60 * 1000));
        return expire > 60;
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}
//正常情况下 返回的是一个小数点后面两位数的数字 而不是获取到一个整数 但是最后面的是0 的时候 就会直接吧0 却掉 比如 返回值可能为 0.34 ,0.4,不可能为0.40)
public static String StringFormatDouble(String str) {
    double f = Float.parseFloat(str);
    DecimalFormat df = new DecimalFormat("#0.00");
    String info = df.format(f);
    if (info.endsWith(".0") || info.endsWith(".00"))
        info = info.substring(0, info.indexOf("."));
    if (info.indexOf(".") > 0 && info.length() > 1 && info.endsWith("0"))
        info = info.substring(0, info.length() - 1);
    return info;
}

//string 转换成一个小数点后面两位的 double类型z(后面没有俩位数  直接添加 .00 )
public static String StringFormatDouble00(String str) {
    double f = Float.parseFloat(str);
    //DecimalFormat 这个是四舍五入
    DecimalFormat df = new DecimalFormat("#0.00");
    return df.format(f);
}

//string 转换成一个整数  (都包含了四舍五入)
public static String StringFormatInt(String str) {
    double f = Float.parseFloat(str);
    DecimalFormat df = new DecimalFormat("#0");
    return df.format(f);
}

//string 转换成一个整数  (不包含了四舍五入)
public static int StringFormatInt2(String str) {
    if (str != null && !str.equals("")) {
        double f = Float.parseFloat(str);
        int mInt = (int) f;
        return mInt;
    } else {
        return 0;
    }
}

//正常情况下 返回的是一个小数点后面两位数的数字 而不是获取到一个整数 但是最后面的是0 的时候 就会直接吧0 却掉 比如 返回值可能为 0.34 ,0.4,不可能为0.40)
public static float StringFormatFloat(String str) {
    if (str.equals(""))
        return 0;
    double f = Float.parseFloat(str);
    DecimalFormat df = new DecimalFormat("#0.00");
    String info = df.format(f);
    if (info.endsWith(".0") || info.endsWith(".00"))
        info = info.substring(0, info.indexOf("."));
    if (info.indexOf(".") > 0 && info.length() > 1 && info.endsWith("0"))
        info = info.substring(0, info.length() - 1);
    return Float.parseFloat(info);
}
/**
 * 比较时间差 1天
 *
 * @param time
 * @return
 */
@SuppressLint("SimpleDateFormat")
public static boolean isExpire(long time) {
    long overtime = time * 1000;
    long currenttime = System.currentTimeMillis();
    int expire = (int) ((overtime - currenttime) / (60 * 60 * 1000 * 24));
    return expire <= 1;
}
public class DateUtil {

    public static String parseDate2Str(Long d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date(d));
    }

    public static String parseDate2Str(Long d, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(d));
    }

    public static Long parseDate2Second(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d = sdf.parse(date);
            return d.getTime() / 1000L;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static String parseTimelineDate(long parseDate) {
        long s = System.currentTimeMillis() / 1000 - parseDate;
        if (s < 10) {
            return "刚刚";
        } else if (s < 60) {
            return s + "秒前";
        } else if (s < 60 * 60) {
            return s / 60 + "分钟前";
        } else if (s < 60 * 60 * 24) {
            return s / (60 * 60) + "小时前";
        } else {
            return parseDate2Str(parseDate * 1000, "MM-dd HH:mm");
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值