日期,小数,项目工具类

项目中经常用到的一些小方法:
方便,快捷,能快速提高我们的开发效率
类名: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 Object obj = new Object();
    //得到当前的时间
    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, "yyyyMMddHHmmss");
    }

    //把指定字符(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(){
       Calendar calendar = Calendar.getInstance();
       calendar.setTime(new Date());
       calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)-1);
       return getDateFormate(calendar.getTime(),"MMdd");
    }

    /**
     * 
     *********************************************************.<br>
     * [方法] getYestertodayMMDD <br>
     * [描述] 获取昨天日期  MMdd格式 <br>
     * [参数] TODO(对参数的描述) <br>
     * [返回] String <br>
     * [时间] 2016-4-19 上午10:33:00 <br>
     *********************************************************.<br>
     */
    public static String getYestertodayyMMdd(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){
        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));  
        return  b1.add(b2).doubleValue();
    }

    /**
     * ********************************************************
     * @Description: 小数的相减
     * @param str
     * @return String
     * @date 2014-11-1 下午05:31:59
     ********************************************************
     */
    public static Double reducedouble(Double double1 ,Double double2){
        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));  
        return  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));
    }
    /**
     * ********************************************************
     * @Description: 小数的相减
     * @param str
     * @return String
     * @date 2014-11-1 下午05:31:59 
     ********************************************************
     */
    public static Double mindouble(Double double1 ,Double double2){
        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));  
        return  b1.subtract(b2).doubleValue();
    }   
    /**
     * ********************************************************
     * @Description: 小数的相除
     * @param str
     * @return String
     * @date 2014-11-1 下午05:31:59 
     ********************************************************
     */
    public static Double divisiondouble(Double double1 ,Double double2){
        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));  
        return  b1.divide(b2,5,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
    /**
     * ********************************************************
     * @Description: 小数的相乘
     * @param str
     * @return String
     * @date 2014-11-1 下午05:31:59 
     ********************************************************
     */
    public static Double muldouble(Double double1 ,Double double2){
        BigDecimal b1 = new BigDecimal(Double.toString(double1.doubleValue()));  
        BigDecimal b2 = new BigDecimal(Double.toString(double2.doubleValue()));  
        return  b1.multiply(b2).doubleValue();
    }
    /**
     ********************************************************* .<br>
     * [方法] isnull <br>
     * [描述] 判断是否为空 <br>
     * [参数] 参数1,参数2,参数3 <br>
     * [返回] String <br>
     * [时间] 2016-3-10 上午10:44:27 <br>
     ********************************************************* .<br>
     */
    public static Boolean isnull(Object idvalue) {
        Boolean falg = false;
        if (null == idvalue || idvalue.equals("") || "null".equals(idvalue)) {
            falg = true;
        }
        return falg; 
    }

    /**
     ********************************************************* .<br>
     * [方法] appendField <br>
     * [描述] 拼接字符串 <br>
     * [参数] 参数1,参数2 <br>
     * [返回] String <br>
     * [时间] 2016-3-10 上午10:43:53 <br>
     ********************************************************* .<br>
     */
    public static String sbuadd(String one, String two) {
        return new StringBuffer(one).append(two).toString();
    }
    /**
     * 
     *********************************************************.<br>
     * [方法] getProperty <br>
     * [描述] TODO(根据服务器类型取不同地址) <br>
     * [参数] TODO(文件名,路径key) <br>
     * [返回] String <br>
     * [时间] 2018-2-8 上午10:58:29 <br>
     * [作者] 李超 【lc】
     *********************************************************.<br>
     */
    public static String getTypeUrl(String name, String url){
        ResourceBundle rb=ResourceBundle.getBundle(name);
        String reurl = rb.getString("server")+url;
        return rb.getString(reurl);
    }
    /**
     * 
     *********************************************************.<br>
     * [方法] getProperty <br>
     * [描述] TODO(这里用一句话描述这个方法的作用) <br>
     * [参数] TODO(对参数的描述) <br>
     * [返回] String <br>
     * [时间] 2018-2-8 上午10:58:29 <br>
     * [作者] 李超 【lc】
     *********************************************************.<br>
     */
    public static String getUrl(String name, String url){
        ResourceBundle rb=ResourceBundle.getBundle(name);
        return rb.getString(url);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_25384901/article/details/71469119