1. // 精度会失真
    2. double c=0.1+0.2;
    3. System.out.println("c = " + c);
    4. // 转成字符串传入
    5. BigDecimal a=new BigDecimal("0.1");
    6. // 使用valueof方法
    7. BigDecimal b=BigDecimal.valueOf(0.2);
    8. BigDecimal c1=a.add(b); //结果不失真 //+
    9. System.out.println("c1 = " + c1);
    10. // 转double类型用来传输
    11. Double rs=c1.doubleValue();
    12. System.out.println("rs = " + rs);
    13. //除法,参数:除数,保留小数位,舍入模式
    14. BigDecimal c2 =a.divide(b,4,BigDecimal.ROUND_HALF_UP);
    1. //格式化日期
    2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    3. String dateString = "2022-02-15 09:29:29";
    4. // 处理对象为字符串,先转成标准格式的日期
    5. Date date = sdf.parse(dateString);
    6. //再通过标准格式的日期转换成标准格式的字符串
    7. String dateNormal = sdf.format(date);
    8. System.out.println(dateNormal);
    1. LocalDate today = LocalDate.now();
    2. System.out.println("today = " + today);
    3. System.out.println("today.getYear() = " + today.getYear());
    4. System.out.println("today.getDayOfMonth() = " + today.getDayOfMonth());
    5. System.out.println("today.getDayOfWeek() = " + today.getDayOfWeek().getValue());
    6. System.out.println("today.getMonthValue() = " + today.getMonthValue());
    7. LocalDateTime time = LocalDateTime.now();
    8. System.out.println("time = " + time);
    9. MonthDay nowd = MonthDay.from(time);
    10. System.out.println("nowd = " + nowd);
    1. Instant n = Instant.now();
    2. System.out.println("n = " + n);
    3. long time = n.getEpochSecond();
    4. System.out.println("time = " + time);
    5. Date date = Date.from(n);
    6. System.out.println("date = " + date);
    7. LocalDateTime ldt=LocalDateTime.now();
    8. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    9. System.out.println("dtf.format(date) = " + dtf.format(ldt));
    10. System.out.println("ldt.format(dtf) = " + ldt.format(dtf));
    11. //解析字符串
    12. DateTimeFormatter ymdhms=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    13. LocalDate ldt1=LocalDate.parse("2202-08-09",dtf);
    14. LocalDateTime ldt2=LocalDateTime.parse("2202-08-09 12:14:38",ymdhms);
    15. System.out.println("ldt1 = " + ldt1);
    16. System.out.println("ldt2 = " + ldt2);
    1. public static void testChromoUnitsPlus() {
    2. //Get the current date
    3. LocalDate today = LocalDate.now();
    4. System.out.println("今天: " + today);
    5. //add 1 week to the current date
    6. LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
    7. System.out.println("下周: " + nextWeek);
    8. //add 1 month to the current date
    9. LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
    10. System.out.println("下个月: " + nextMonth);
    11. //add 1 year to the current date
    12. LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
    13. System.out.println("明年: " + nextYear);
    14. //add 10 years to the current date
    15. LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
    16. System.out.println("10年后: " + nextDecade);
    17. }