java 日期处理

java 日期处理

一,日期和字符串之间转换

1, string 转换为 double 并四舍五入保留两位小数

    /**
     * string  类型转换为 double   并四舍五入保留两位小数
     * @param str
     * @return
     */
    public static double stringChangeDouble(String str) {
        double d = Double.parseDouble(str);
        System.out.println("d is " + d);
        BigDecimal decimal = BigDecimal.valueOf(d);
        DecimalFormat df = new DecimalFormat("0.##");
        df.setRoundingMode(RoundingMode.HALF_UP);
        String format = df.format(decimal.doubleValue());
        return Double.parseDouble(format);
    }

2,double 转 string

/**
     * double  转  string
     * @param d
     * @return
     */
    public static String doubleChangeString(double d) {
        String s = String.valueOf(d);// valueOf(String) method
        System.out.println("s is " + s);
        return s;
    }

3, double 保留两位小数

    /**
     * double   保留两位小数
     * @param d
     * @return
     */
    public static double doubleRound(double d) {
        BigDecimal decimal = BigDecimal.valueOf(d);
        DecimalFormat df = new DecimalFormat("0.##");
        df.setRoundingMode(RoundingMode.HALF_UP);
        String format = df.format(decimal.doubleValue());
        System.out.println(Double.parseDouble(format) + "-------返回的double--------");
        return Double.parseDouble(format);
    }

4, string 不足几位补0

   /**
     * string 不足几位补0
     * @param str
     * @param strLength
     * @return
     */
    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                // sb.append(str).append("0");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

5, string 字符串数字 转换为百分数

 /**
     * string  字符串数字  转换为百分数
     *
     * @param param
     * @return
     */
    public static String stringChangePercent(String param) {
        double v = stringChangeDouble(param) * 100;
        String value = v + "%";
        return value;
    }

6 , 比较double值是否相等

   /**
     * 比较double值是否相等
     * @param a
     * @param b
     * @return
     */
    public static Boolean doubleEquality(double a, double b) {
        System.out.println(Math.abs(a - b));
        if (Math.abs(a - b) < 0.000000002) {
           return true;
        }else {
            return  false;
        }
    }

7, double 保留十位小数

 /**
     * double   保留十位小数
     *
     * @param d
     * @return
     */
    public static String doubleRoundChangString(double d) {
        BigDecimal decimal = BigDecimal.valueOf(d);
        DecimalFormat df = new DecimalFormat("0.##########");
        df.setRoundingMode(RoundingMode.HALF_UP);
        String format = df.format(decimal.doubleValue());
        System.out.println(Double.parseDouble(format) + "-------返回的double--------");
        double v = Double.parseDouble(format);
        String s = String.valueOf(v);// valueOf(String) method
        System.out.println("s is " + s);
        return  s;
    }

8, 不够位数的在前面补0,保留num的长度位数字

/**
     * 不够位数的在前面补0,保留num的长度位数字
     * @param code
     * @return
     */
    public static  String autoGenericCode(String code, int num) {
        String result = "";
        // 保留num的位数
        // 0 代表前面补充0
        // num 代表长度为4
        // d 代表参数为正数型
        result = String.format("%0" + num + "d", Integer.parseInt(code) + 1);
        return result;
    }

9, 正负数转换

 /**
     * 正负数转换
     *
     * @return
     */
    public static Double zFChange(Double d) {
        if (d != 0d) {
            d = -d;
        }
        return d;
    }

10, String转换为yyyy-MM-dd日期

   /**
     * String转换为yyyy-MM-dd日期
     *
     * @return
     */
    public static Date strToDate(String dateStr) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dateStr, pos);
        return date;
    }

11,日期转换为 例如:20210407

    /**
     * 日期转换为 例如:20210407
     *
     * @return
     */
    public static String getNowDateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date2 = sdf.format(date);
        System.out.println(date2);
        return date2;
    }

二,日期转换

1, 获取String类型的纯数字年月日 例如:20210407

   /**
     * 获取String类型的纯数字年月日   例如:20210407
     *
     * @return
     */
    public static String getNowDateToString() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date2 = sdf.format(date);
        System.out.println(date2);
        return date2;
    }

2, 指定日期加上天数后的日期

  /**
     * 指定日期加上天数后的日期
     *
     * @param num      为增加的天数
     * @param currdate newDate 创建时间(增加前的日期)
     * @return
     * @throws ParseException
     */
    public static String plusDay(int num, Date currdate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("现在的日期是:" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.setTime(currdate);
        ca.add(Calendar.DATE, num);// num为增加的天数,可以改变的
        currdate = ca.getTime();

        String enddate = format.format(currdate);
        System.out.println("增加天数以后的日期:" + enddate);
        return enddate;
    }

3, 获取指定日期是星期几

  /**
     * 获取指定日期是星期几
     *
     * @param date
     * @return 指定日期是星期几
     */
    public static String getWeekOfDate(Date date) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println(w + "-----index");
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

4, 获取指定日期是星期几

 /**
     * 获取指定日期是星期几
     *
     * @param date
     * @return 1-7      1就是星期一
     */
    public static int getWeekOfDateIndex(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println(index + "-----index");
        return index;
    }

5, 日期到下一个月的1号

  /**
     * 日期到下一个月的1号
     *
     * @param date
     * @return
     */
    public static String dateGoNextOne(Date date) {
        String endDate = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String newDate = format.format(date);
        String[] dates = newDate.split("-");
        int a = Integer.parseInt(dates[1]);
        int year = Integer.parseInt(dates[0]);
        //当月份等于12,跨年
        if (a == 12) {
            a = 1;
            year++;
        } else {
            a++;
        }
        endDate = year + "-" + a + "-01";
        return endDate;
    }

6, 时间戳转换为yyyy-MM-dd格式

  /**
     * 时间戳转换为yyyy-MM-dd格式
     * @param time
     * @return
     */
    public static String changeTimeToYearMonthDay(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sdf.format(new Date(Long.parseLong(time)));
        return sd;
    }

7, 当前年月 yyyy-MM格式

   /**
     * 当前年月 yyyy-MM格式
     * @param
     * @return
     */
    public static String getCurrentDate2() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String sd = sdf.format(new Date());
        return sd;
    }

8, 上一个月 yyyy-MM格式

 public  static String getLastMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

9, 获取过去时间点

 /**
     * 获取过去时间点
     *
     * @param type
     * @return
     */
    public static long getLastPeroid(int type, Date date) {
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String day = format.format(Date);
        Calendar c = Calendar.getInstance();
        if (0 == type) {
            //过去1天
            c.setTime(date);
            c.add(Calendar.DATE, -1);
            Date d = c.getTime();
            return d.getTime();
        } else if (1 == type) {
            //过去7天
            c.setTime(date);
            c.add(Calendar.DATE, -7);
            Date d = c.getTime();
            return d.getTime();
        } else if (2 == type) {
            //过去一月
            c.setTime(date);
            c.add(Calendar.MONTH, -1);
            Date m = c.getTime();
            return m.getTime();
        } else if (3 == type) {
            //过去一年
            c.setTime(date);
            c.add(Calendar.YEAR, -1);
            Date y = c.getTime();
            return y.getTime();
        } else if (4 == type) {
            //过去二十年
            c.setTime(date);
            c.add(Calendar.YEAR, -20);
            Date y = c.getTime();
            return y.getTime();
        }
        //过去1天
        c.setTime(date);
        c.add(Calendar.DATE, -1);
        Date d = c.getTime();
        return d.getTime();
    }

10, 本月第一天

 /**
     * 本月第一天
     * @return
     */
    public static String getThisMonthStartDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 本月起始
        Calendar thisMonthFirstDateCal = Calendar.getInstance();
        thisMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
        String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime());
//        String thisMonthFirstTime = format.format(thisMonthFirstDateCal.getTime()) + " 00:00:00";
        System.out.println("本月起始:" + thisMonthFirstTime);
        return thisMonthFirstTime;
    }

11,本月最后一天

   /**
     * 本月最后一天
     * @return
     */
    public static String getThisMonthEndDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");     // 本月末尾

        Calendar thisMonthEndDateCal = Calendar.getInstance();
        thisMonthEndDateCal.set(Calendar.DAY_OF_MONTH, thisMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
//        String thisMonthEndTime = format.format(thisMonthEndDateCal.getTime()) + " 23:59:59";
        String thisMonthEndTime = format.format(thisMonthEndDateCal.getTime()) ;
        System.out.println("本月末尾:" + thisMonthEndTime);
        return thisMonthEndTime;
    }

12, 上月第一天

    /**
     * 上月第一天
     * @return
     */
    public static String getLastMonthStartDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // 上月起始
        Calendar lastMonthFirstDateCal = Calendar.getInstance();
        lastMonthFirstDateCal.add(Calendar.MONTH,-1);
        lastMonthFirstDateCal.set(Calendar.DAY_OF_MONTH, 1);
//        String lastMonthFirstTime = format.format(lastMonthFirstDateCal.getTime()) + " 00:00:00";
        String lastMonthFirstTime = format.format(lastMonthFirstDateCal.getTime());
        System.out.println("上月起始:" + lastMonthFirstTime);
        return lastMonthFirstTime;
    }

13, 上月最后一天

  /**
     * 上月最后一天
     * @return
     */
    public static String getLastMonthEndDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");     // 本月末尾
        // 上月末尾
        Calendar lastMonthEndDateCal = Calendar.getInstance();
        lastMonthEndDateCal.add(Calendar.MONTH,-1);
        lastMonthEndDateCal.set(Calendar.DAY_OF_MONTH, lastMonthEndDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
//        String lastMonthEndTime = format.format(lastMonthEndDateCal.getTime()) + " 23:59:59";
        String lastMonthEndTime = format.format(lastMonthEndDateCal.getTime());
        System.out.println("上月末尾:" + lastMonthEndTime);
        return lastMonthEndTime;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值