欢迎光临
我们一直在努力

Java-LocalDateTime类和BigDecimal类

localdatetime和localdate有什么区别

LocalDateTime 和 LocalDate 是 Java 8 中 java.time 包下的两个核心类,主要区别在于表示的日期时间粒度不同:

  • LocalDate:只存储日期(年月日),不包含时间和时区。

    • 例:2025-05-28

  • LocalDateTime:存储日期 + 时间(年月日时分秒纳秒),同样不包含时区。

    • 例:2025-05-28T14:30:15.123


  • 详细对比

    特性LocalDateLocalDateTime
    信息粒度 年月日 年月日 + 时分秒纳秒
    典型格式 yyyy-MM-dd yyyy-MM-ddTHH:mm:ss
    构造示例 LocalDate.of(2025, 5, 28) LocalDateTime.of(2025, 5, 28, 14, 30)
    获取当前值 LocalDate.now() → 2025-05-28 LocalDateTime.now() → 2025-05-28T14:30:15.123
    解析字符串 LocalDate.parse("2025-05-28") LocalDateTime.parse("2025-05-28T14:30:15")
    常用场景 生日、节假日、账单日 订单创建时间、日志时间戳

    转换方法

    两者可以互相转换:

    LocalDate date = LocalDate.now(); // 2025-05-28
    LocalDateTime dateTime = date.atStartOfDay(); // 2025-05-28T00:00
    LocalDateTime dateTime2 = date.atTime(14, 30, 15); // 2025-05-28T14:30:15

    // 从 LocalDateTime 提取 LocalDate
    LocalDate dateBack = dateTime.toLocalDate(); // 2025-05-28


    注意事项

    • 都不包含时区:如果需要处理时区(如数据库 TIMESTAMP WITH TIME ZONE 或跨时区业务),应使用 ZonedDateTime 或 OffsetDateTime。

    • 数据库映射:JPA 2.2 以上可以直接使用;旧版本可能需要配置转换器或使用 java.sql.Date / Timestamp。

    • 比较时间:LocalDate 只能比较到天;LocalDateTime 可以比较到纳秒级。

    1,LocalDateTime类

    官方手册

    Local Date Time只有一个私有的构造方法

    1.1 创建LocalDateTime 对象

    //当前时间
    LocalDateTime now = LocalDateTime.now();
    //指定时间
    LocalDateTime localDateTime = LocalDateTime.of(2026,6,5,16,30);
    //字符串转时间
    LocalDateTime parse = LocalDateTime.parse("2026-06-05T16:30:00");
    System.out.println(now);
    System.out.println(localDateTime);
    System.out.println(parse);

    获取当前年月日

    LocalDateTime now = LocalDateTime.now();
    int year = now.getYear();
    int month = now.getMonthValue();
    int day = now.getDayOfMonth();
    int hour = now.getHour();
    int minute = now.getMinute();
    int second = now.getSecond();

    System.out.println(" 年: "+year);
    System.out.println(" 月: "+month);
    System.out.println(" 日: "+day);
    System.out.println(" 时: "+hour);
    System.out.println(" 分: "+minute);
    System.out.println(" 秒: "+second);

    获取具体详细信息

    LocalDateTime now = LocalDateTime.now();
    System.out.println("当前时间" + now);
    System.out.println("本周周几" + now.getDayOfWeek().getValue());
    System.out.println("当月第几天" + now.getDayOfMonth());
    System.out.println("当年第几天" + now.getDayOfYear());

    日期运算

    增减,减少天数

    LocalDateTime now = LocalDateTime.now();
    LocalDateTime newDatePlus = now.plusDays(1);
    System.out.println("增加一天的日期" + newDatePlus);

    LocalDateTime newDateMinus = now.minusDays(1);
    System.out.println("减少一天的日期" + newDateMinus);

    LocalDateTime newWeekPlus = now.plusWeeks(1);
    System.out.println("增加一周的日期" + newWeekPlus);

    LocalDateTime newWeekMinus = now.minusWeeks(1);
    System.out.println("减少一周的日期" + newWeekMinus);

    年和月与上面一样

    根据当前时间获取指定时间

    • previousOrSame:寻找当前日期或之前最近的指定星期几
    • nextOrSame:寻找当前日期或之后最近的指定星期几

    LocalDateTime currentDate = LocalDateTime.now();
    LocalDateTime firstDayOfWeek =
    currentDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    LocalDateTime lastDayOfWeek =
    currentDate.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
    System.out.println("这周的星期一" + firstDayOfWeek);
    System.out.println("这周的星期天" + lastDayOfWeek);

    获取当前日期所在月的第一天和最后一天的日期

    public static void main(String[] args) {
    LocalDateTime currentDate = LocalDateTime.now();
    LocalDateTime firstDayOfMonth =
    currentDate.with(TemporalAdjusters.firstDayOfMonth());

    LocalDateTime lastDayOfMonth =
    currentDate.with(TemporalAdjusters.lastDayOfMonth());

    System.out.println(firstDayOfMonth);
    System.out.println(lastDayOfMonth);
    }

    2,BigDecimal 类

    BigDecimal 是 Java在java.math包中提供的 线程安全 的API类,用来对超过16位有效位的数进行精确的运算。双精度浮点型变量doble可以处理16位有效数,但在实际应用中,可能需要对更大或者更小的数进行运算和处理。官方手册 Decial(小数,十进制)

    public static void main(String[] args) {
    BigDecimal doubleNum =new BigDecimal(1.99);
    System.out.println( doubleNum);
    BigDecimal stringNum = new BigDecimal("2.99");
    System.out.println( stringNum);
    }
    //输出结果:

    1.9899999999999999911182158029987476766109466552734375
    2.99

    使用new BigDecimal(1.99)时,实际上是将一个已经被舍入的double值传递给了 BigDecial。BigDecial 然后精确地表示了这个已经不精确的double值

    2.1 加减乘

    public static void main(String[] args) {
    BigDecimal a =new BigDecimal("1.35");
    BigDecimal b = new BigDecimal("3.22");

    BigDecimal addRet = a.add(b);
    System.out.println(addRet);

    BigDecimal subRet = a.subtract(b);
    System.out.println(subRet);

    BigDecimal mulRet = a.multiply(b);
    System.out.println(mulRet);

    }

    参与运算后会生成新的BigDecial 对象

    2.2 除

    public static void main(String[] args) {
    BigDecimal a =new BigDecimal("1.35");
    BigDecimal b = new BigDecimal("3.22");
    BigDecimal divRet = a.divide(b);
    System.out.println(divRet);
    }

    BigDecimal的divide⽅法在进⾏除法运算时,如果结果是⼀个⽆限循环⼩数,就会抛出 ArithmeticException 异常

    指定精度和舍⼊模式

    public static void main(String[] args) {
    BigDecimal a =new BigDecimal("1.35");
    BigDecimal b = new BigDecimal("3.22");
    BigDecimal divRet = a.divide(b, 4, RoundingMode.HALF_UP);
    System.out.println(divRet);
    }
    //输出结果 0.4193

    • 4表⽰除法运算结果的⼩数部分保留4位
    • RoundingMode.HALF_UP代表四舍五⼊

    使⽤MathContext

    public static void main(String[] args) {
    BigDecimal a = new BigDecimal("1.35");
    BigDecimal b = new BigDecimal("3.22");

    BigDecimal divRet = a.divide(b, new MathContext(4,RoundingMode.HALF_UP));
    System.out.println(divRet);
    }
    //输出结果:
    0.4193

    newMathContext(4,RoundingMode.HALF_UP),若不指定的情况下默认是: RoundingMode.HALF_UP模式。

    赞(0)
    未经允许不得转载:171主机测评 » Java-LocalDateTime类和BigDecimal类
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址