Java&LocalDate&LocalTime&LocalDateTime

LocalDate

LocalDate today = LocalDate.now(); // 获取当前日期
LocalDate specificDate = LocalDate.of(2022, 4, 7); // 指定日期
int year = today.getYear(); // 获取年份
int month = today.getMonthValue(); // 获取月份
int day = today.getDayOfMonth(); // 获取日期

LocalTime

LocalTime now = LocalTime.now(); // 当前时间
LocalTime specificTime = LocalTime.of(14, 30, 0); // 指定时间
int hour = now.getHour(); // 获取小时
int minute = now.getMinute(); // 获取分钟
int second = now.getSecond(); // 获取秒数

LocalDateTime

LocalDateTime currentDateTime = LocalDateTime.now(); // 当前日期时间
LocalDateTime specificDateTime = LocalDateTime.of(2022, 4, 7, 14, 30, 0); // 指定日期时间
LocalDate date = currentDateTime.toLocalDate(); // 提取日期部分
LocalTime time = currentDateTime.toLocalTime(); // 提取时间部分

日期转换成String

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(" HH:mm:ss");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  • 月份(MM)必须大写

  • 另外小时(HH是24小时制,hh是12小时,'a’如果不加在后面难以区分上午还是下午)

// 使用24小时制
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter24 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime24 = dateTime.format(formatter24);
System.out.println("24小时制:" + formattedDateTime24);

// 使用12小时制
DateTimeFormatter formatter12 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a");
String formattedDateTime12 = dateTime.format(formatter12);
System.out.println("12小时制:" + formattedDateTime12);

//输出:
24小时制:2024-04-07 20:18:46
12小时制:2024-04-07 08:18:46 下午

String转日期

第一种:

LocalDateTime dateTime = LocalDateTime.parse("2022-04-07T15:30:20");
        LocalDate date = LocalDate.parse("2022-04-07");
        LocalTime time = LocalTime.parse("15:30:20");
        System.out.println(dateTime);
        System.out.println(date);
        System.out.println(time);

//输出:
2022-04-07T15:30:20
2022-04-07
15:30:20

第二种:

String strDateTime = "2021-03-21 13:20:10";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(strDateTime, formatter);
System.out.println(dateTime);


//输出:
2021-03-21T13:20:10

判断某个日期是否存在:

 public static boolean isValidDate(int year, int month, int day) {
        try {
            LocalDate.of(year, month, day);
            return true;
        } catch (java.time.DateTimeException e) {
            return false;
        }
    }

其他方法

  • int getDayOfMonth():获取月份中的日期。
  • int getDayOfYear():获取年份中的日期。
  • DayOfWeek getDayOfWeek():获取星期几。
  • int getHour():获取小时数。
  • int getMinute():获取分钟数。
  • int getSecond():获取秒数。
  • LocalDateTime plusYears(long years):增加指定的年数。
  • LocalDateTime plusMonths(long months):增加指定的月数。
  • LocalDateTime plusDays(long days):增加指定的天数。
  • LocalDateTime plusHours(long hours):增加指定的小时数。
  • LocalDateTime plusMinutes(long minutes):增加指定的分钟数。
  • LocalDateTime plusSeconds(long seconds):增加指定的秒数。
  • LocalDateTime plusNanos(long nanos):增加指定的纳秒数。
  • boolean isBefore(LocalDateTime other):判断当前日期时间是否在指定日期时间之前。
  • boolean isAfter(LocalDateTime other):判断当前日期时间是否在指定日期时间之后。
  • boolean isEqual(LocalDateTime other):判断当前日期时间是否与指定日期时间相等。
  • String format(DateTimeFormatter formatter):将日期时间格式化为字符串。
  • 14
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值