Java8中 LocalDateTime toLocalDate()用法及代码示例
-- LocalDateTime类的toLocalDate()方法用于获取此LocalDateTime的LocalDate表示形式。
1. LocalDate只保存年月日
2. LocalDateTime保存年月日时分秒。
常用Api(1.)
package cn.js;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.Date;
/**
* @Description:
* @Author: com.js
* @CreateTime: 2023-04-23 09:47
* @Version: 1.0
* @introduce:
*/
public class Test02 {
public static void main(String[] args) {
LocalDateTime dt = LocalDateTime.now();
System.out.println("LocalDateTime时间:"+dt);//2023-04-23T09:58:58.914
LocalDate createDate = dt.toLocalDate();
System.out.println("通过toLocalDate装换:"+createDate);//2023-04-23
Month month = createDate.getMonth();//获取月份英文
System.out.println("month: "+month);//APRIL
int value = createDate.getMonth().getValue();
System.out.println("value: "+value);//4
System.out.println("-------------");
LocalDate createDate2 = dt.toLocalDate();
System.out.println("新的"+createDate2);
String s = createDate2.toString();
System.out.println("s: "+s);//截取 20230423
String replace = s.replace("-", "");
System.out.println("截取:"+replace);
int year = createDate2.getYear();//获取年份 2023
System.out.println("year: "+year);
}
}
常用Api(2.)
package cn.js;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
/**
* @Description:
* @Author: com.js
* @CreateTime: 2023-04-23 10:36
* @Version: 1.0
* @introduce:
*/
public class Test03 {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println("今天是 "+now);//今天是 2023-04-23
System.out.println("1970年到现在一共 "+now.toEpochDay() +" 天");//1970年到现在一共 19470 天
final int lengthOfYear = now.lengthOfYear();
System.out.println("今年一共 "+lengthOfYear+" 天");//今年一共 365 天
final int lengthOfMonth = now.lengthOfMonth();
System.out.println("本月一共 "+ lengthOfMonth +" 天");//本月一共 30 天
final boolean leapYear = now.isLeapYear();
System.out.println("今年是否是闰年:"+leapYear);//今年是否是闰年:false
final LocalDate firstDayOfMonth = now.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("本月的第一天是 : "+firstDayOfMonth);//本月的第一天是 : 2023-04-01
//下一个周一
final LocalDate withMONDAY = now.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println("下周一日期是 :"+withMONDAY);//下周一日期是 :2023-04-24
System.out.println(" 日期在当前时间之后: "+ withMONDAY.isAfter(now));//日期在当前时间之后: true
System.out.println(" 日期在当前时间之前: "+ withMONDAY.isBefore(now));//日期在当前时间之前: false
//最后一个周一
final LocalDate lastMONDAY = now.with(TemporalAdjusters.lastInMonth(DayOfWeek.TUESDAY));
System.out.println("本月最后一个周二是 :"+lastMONDAY);//本月最后一个周二是 :2023-04-25
final LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("本月最后一天是 : "+lastDay);//本月最后一天是 : 2023-04-30
// 加一年
final LocalDate plusYears = now.plusYears(1);
System.out.println("当前日期加一年 : "+plusYears);//当前日期加一年 : 2024-04-23
//两个日期相差天数 两个日期相差天数:366
System.out.println("两个日期相差天数:"+(plusYears.toEpochDay() - now.toEpochDay()));
final LocalDate plusMonths1 = now.plusMonths(12);
System.out.println("当前日期加12 个月 :"+plusMonths1);//当前日期加12 个月 :2024-04-23
final LocalDate minusDays = now.minusDays(1);
System.out.println("当前日期减 1 天 : "+minusDays);//当前日期减 1 天 : 2023-04-22
final LocalDate plusDays = now.plusDays(1);
System.out.println("当前日期加 1 天 : "+plusDays);//当前日期加 1 天 : 2023-04-24
final int dayOfMonth = now.getDayOfMonth();
System.out.println("今天是这个月的第 "+dayOfMonth +" 天");//今天是这个月的第 23 天
final int monthValue = now.getMonthValue();
System.out.println("本月是今年的第 "+monthValue + "月");//本月是今年的第 4月
final Month month = now.getMonth();
System.out.println("本月的英文 : "+month);//本月的英文 : APRIL
// 本周的周几
final DayOfWeek dayOfWeek = now.getDayOfWeek();
System.out.println("今天是周几英文: " + dayOfWeek);//今天是周几英文: SUNDAY
System.out.println("今天是本周周几: " + dayOfWeek.getValue());//今天是本周周几: 7
// string 转 localDate
final LocalDate parse = LocalDate.parse("2022-07-12");//2022-07-12
final LocalDate parse1 = LocalDate.parse("2023-07-12", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(parse1);//2023-07-12
System.out.println(" 转日期 "+parse);//转日期 2023-07-12
System.out.println("DateTimeFormatter 转日期 "+parse1); //DateTimeFormatter 转日期 2023-07-12
//获取指定日期
final LocalDate startDate = LocalDate.of(2023 , 6, 30);
System.out.println(startDate);//2023-06-30
final LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("当前日期时间:"+nowDateTime);//当前日期时间:2023-04-23T10:36:25.345
final LocalTime localTime = LocalTime.now();
System.out.println("当前时间: "+localTime);//当前时间: 10:36:25.345
final String format = nowDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a"));
System.out.println("当前日期时间 格式化"+format);//当前日期时间 格式化2023-04-23 10:36:25 上午
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/188536.html