日期,小数,项目工具类

来源:互联网 发布:网络引擎优化 编辑:程序博客网 时间:2024/06/08 08:49

项目中经常用到的一些小方法:
方便,快捷,能快速提高我们的开发效率
类名:DateUtil

package net.ttxs.util;import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;public class DateUtil {    //得到当前的时间    public static Date getDate() {        Calendar canlendar = Calendar.getInstance();        return canlendar.getTime();    }    //提到指定的millis得到时间    public static Date getDate(long millis) {        Calendar canlendar = Calendar.getInstance();        canlendar.clear();        canlendar.setTimeInMillis(millis);        return canlendar.getTime();    }    public static long getMillis() {        return Calendar.getInstance().getTimeInMillis();    }    //得到指定日期的字符串(yyyy-MM-dd HH:mm:ss.SSS)    public static String getDateFormate(Date date, String formate) {        try {            SimpleDateFormat simpleDateFormate = new SimpleDateFormat(formate);            return simpleDateFormate.format(date);        } catch (Exception e) {        }        return "";    }    //根据日期得到YYYY-MM-DD HH:MM:SS.SSS格式字符串    public static String get4yMdHmsS(Date date) {        return DateUtil.getDateFormate(date, "yyyy-MM-dd HH:mm:ss.SSS");    }    //根据日期得到YYYY-MM-DD HH:MM:SS格式字符串    public static String get4yMdHms(Date date) {        return DateUtil.getDateFormate(date, "yyyy-MM-dd HH:mm:ss");    }    //根据日期得到YYYY-MM-DD HH:MM格式字符串    public static String get4yMdHm(Date date) {        return DateUtil.getDateFormate(date, "yyyy-MM-dd HH:mm");    }    //根据日期得到YYYY-MM-DD格式字符串    public static String get4yMd(Date date) {        return DateUtil.getDateFormate(date, "yyyy-MM-dd");    }    //根据日期得到HH:MM:SS格式字符串    public static String get4Hms(Date date) {        return DateUtil.getDateFormate(date, "HH:mm:ss");    }    //根据日期得到YYYYMMDD格式字符串    public static String get4yMdaz(Date date) {        return DateUtil.getDateFormate(date, "yyyyMMdd");    }    //根据日期得到YYMMDD格式字符串    public static String get4yMdad(Date date) {        return DateUtil.getDateFormate(date, "yyMMdd");    }    public static String get4yMdhmsaz(Date date) {        return DateUtil.getDateFormate(date, "yyyyMMdd HH:mm:ss");    }    //把指定字符(yyyy-MM-dd HH:mm:ss.SSS)串转成Date    public static Date parse4yMdHmsS(String sDate) {        return DateUtil.parseDate(sDate, "yyyy-MM-dd HH:mm:ss.SSS");    }    //把指定字符(yyyy-MM-dd HH:mm:ss)串转成Date    public static Date parse4yMdHms(String sDate) {        return DateUtil.parseDate(sDate, "yyyy-MM-dd HH:mm:ss");    }    //把指定字符(yyyy-MM-dd HH:mm)串转成Date    public static Date parse4yMdHm(String sDate) {        return DateUtil.parseDate(sDate, "yyyy-MM-dd HH:mm");    }    //把指定字符(yyyy-MM-dd)串转成Date    public static Date parse4yMd(String sDate) {        return DateUtil.parseDate(sDate, "yyyy-MM-dd");    }    //根据指定格式,把字符串转成日期    public static Date parseDate(String sDate, String formate) {        SimpleDateFormat simpleDateFormate = new SimpleDateFormat(formate);        try {            return simpleDateFormate.parse(sDate);        } catch (ParseException e) {            return null;        }    }    //两个长整型的时间相差(时间的毫秒数),可以得到指定的毫秒数,秒数,分钟数,天数    public static double getDifTwoTime(Date minuendTime, Date subtrahendTime, String tdatestr) {        if (minuendTime == null || subtrahendTime != null) {            return DateUtil.getDifTwoTime(minuendTime.getTime(), subtrahendTime.getTime(), tdatestr);        }        return 0;    }    //两个长整型的时间相差(时间的毫秒数),可以得到指定的毫秒数,秒数,分钟数,天数    public static double getDifTwoTime(long minuendTime, long subtrahendTime, String tdatestr) {        if (tdatestr == null || tdatestr.equals("")) {            tdatestr = "MS";        }        double temp = 1;        /** 毫秒数 */        if ("MS".equalsIgnoreCase(tdatestr)) {            temp = 1;        }        /** 得到秒 */        if ("S".equalsIgnoreCase(tdatestr)) {            temp = 1000;        }        /** 得到分 */        if ("M".equalsIgnoreCase(tdatestr)) {            temp = 1000 * 60;        }        /** 得到小时 */        if ("H".equalsIgnoreCase(tdatestr)) {            temp = 1000 * 60 * 60;        }        /** 得到天 */        if ("D".equalsIgnoreCase(tdatestr)) {            temp = 1000 * 60 * 60 * 24;        }        return (minuendTime - subtrahendTime) / temp;    }    //从日期中得到指定部分(YYYY/MM/DD/HH/SS/SSS)数字    public static int getPartOfTime(Date date, String part) {        Calendar canlendar = Calendar.getInstance();        canlendar.clear();        canlendar.setTime(date);        if (part.equalsIgnoreCase("Y")) {//得到年            return canlendar.get(Calendar.YEAR);        }        if (part.equalsIgnoreCase("M")) {//得到月            return canlendar.get(Calendar.MONTH) + 1;        }        if (part.equalsIgnoreCase("D")) {//得到日            return canlendar.get(Calendar.DAY_OF_MONTH);        }        if (part.equalsIgnoreCase("H")) {//得到时            return canlendar.get(Calendar.HOUR_OF_DAY);        }        if (part.equalsIgnoreCase("M")) {//得到分            return canlendar.get(Calendar.MINUTE);        }        if (part.equalsIgnoreCase("S")) {//得到秒            return canlendar.get(Calendar.SECOND);        }        if (part.equalsIgnoreCase("MS")) {//得到毫秒            return canlendar.get(Calendar.MILLISECOND);        }        return -1;    }    /**     *      *********************************************************.<br>     * [方法] getYestertodayMMDD <br>     * [描述] 获取昨天日期  MMdd格式 <br>     * [参数] TODO(对参数的描述) <br>     * [返回] String <br>     * [时间] 2016-4-19 上午10:33:00 <br>     *********************************************************.<br>     */    public static String getYestertodayMMdd(int day){       Calendar calendar = Calendar.getInstance();       calendar.setTime(new Date());       calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-day);       return getDateFormate(calendar.getTime(),"yyyy-MM-dd");    }    /**     * ********************************************************     * @Description: 小数的相加     * @param str     * @return String     * @date 2014-11-1 下午05:31:59      ********************************************************     */    public static Double adddouble(Double double1 ,Double double2){        DecimalFormat df = new DecimalFormat("######0.00");        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));          BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));          return  new Double(df.format(b1.add(b2).doubleValue()));    }    /**     * ********************************************************     * @Description: 小数的相加     * @param str     * @return String     * @date 2014-11-1 下午05:31:59     ********************************************************     */    public static Double reducedouble(Double double1 ,Double double2){        DecimalFormat    df   = new DecimalFormat("######0.00");        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));          BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));          return  new Double(df.format(b1.subtract(b2).doubleValue()));    }    /**     * ********************************************************     * @Description: 小数的相减     * @param str     * @return String     * @date 2014-11-1 下午05:31:59     ********************************************************     */    public static Double bltwo(Object object){        DecimalFormat    df   = new DecimalFormat("######0.00");         return  new Double(df.format(object));    } /**     *      *********************************************************.<br>     * [方法] appendStringBuff <br>     * [描述] TODO(这里用一句话描述这个方法的作用) <br>     * [参数] TODO(对参数的描述) <br>     * [返回] String <br>     * [时间] 2017-5-9 下午2:46:56 <br>     * [作者] XX 【lc】     *********************************************************.<br>     */    public static String appendStringBuff(Object object1,Object object2){        StringBuffer   bu   = new StringBuffer();         return  bu.append(object1).append(object2).toString();    }}
1 0
原创粉丝点击