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