java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java LocalDateTime类和BigDecimal类

Java中的LocalDateTime类和BigDecimal类详解

作者:01_ice

在Java开发中,LocalDateTime和BigDecimal是处理‌日期时间‌和‌高精度数值运算‌的两个核心类,它们分别解决了传统Date类的线程安全问题以及double/float的精度丢失问题,本文介绍Java中的LocalDateTime类和BigDecimal类,感兴趣的朋友一起看看吧

localdatetime和localdate有什么区别

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

详细对比

特性LocalDateLocalDateTime
信息粒度年月日年月日 + 时分秒纳秒
典型格式yyyy-MM-ddyyyy-MM-ddTHH:mm:ss
构造示例LocalDate.of(2025, 5, 28)LocalDateTime.of(2025, 5, 28, 14, 30)
获取当前值LocalDate.now() → 2025-05-28LocalDateTime.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

注意事项

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);

年和月与上面一样

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

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

使⽤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模式。

到此这篇关于Java中的LocalDateTime类和BigDecimal类详解的文章就介绍到这了,更多相关Java LocalDateTime类和BigDecimal类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文