当前位置:网站首页>自己梳理的LocalDateTime的工具类
自己梳理的LocalDateTime的工具类
2022-07-27 01:07:00 【焦虑的二狗】
参考链接地址:https://mp.weixin.qq.com/s/0HpT1N7eqzVhK0urLUAKlg
package com.example.localdate;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
public class DateUtils {
public static void main(String[] args) {
//获取当前年月日
LocalDate localDate = getNow();
System.out.println(localDate.toString());
System.out.println(localDate.getYear());
System.out.println(localDate.getMonthValue());
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek().getValue());
//获取指定年月日
LocalDate localDate1 = getLocalDate(2022, 7, 24);
System.out.println(localDate1.toString());
System.out.println(localDate1.getYear());
System.out.println(localDate1.getMonthValue());
System.out.println(localDate1.getDayOfMonth());
System.out.println(localDate1.getDayOfWeek().getValue());
//判断日期是否相同
System.out.println(equalsLocalDate(localDate, localDate1));
}
//获取当前年月日
public static LocalDate getNow(){
LocalDate now = LocalDate.now();
return now;
}
public static LocalDate getLocalDate(int year, int month, int day){
LocalDate localDate = LocalDate.of(year, month, day);
return localDate;
}
//判断年月日是否相同
public static boolean equalsLocalDate(LocalDate date1, LocalDate date2){
return date1.equals(date2);
}
//判断月日是否相同,可用于校验是否是生日
public static boolean equalsMonthDay(LocalDate date){
LocalDate now = LocalDate.now();
MonthDay monthDay = MonthDay.of(date.getMonth(), date.getDayOfMonth());
MonthDay currentMonthDay = MonthDay.from(now);
return currentMonthDay.equals(monthDay);
}
//获取当前年月日时分秒
public static LocalTime getNowTime(){
LocalTime now = LocalTime.now();
return now;
}
public static LocalTime addHours(int hours){
return addHours(getNowTime(), hours);
}
public static LocalTime addHours(LocalTime localTime, int hours){
return localTime.plusHours(hours);
}
public static LocalTime addMinutes(int minutes){
return addMinutes(getNowTime(), minutes);
}
public static LocalTime addMinutes(LocalTime localTime, int minutes){
return localTime.plusMinutes(minutes);
}
public static LocalTime addSeconds(int seconds){
return addSeconds(getNowTime(), seconds);
}
public static LocalTime addSeconds(LocalTime localTime, int seconds){
return localTime.plusSeconds(seconds);
}
public static LocalDate addWeek(int week){
return addWeek(getNow(), week);
}
public static LocalDate addWeek(LocalDate localDate, int week){
return localDate.plus(week, ChronoUnit.WEEKS);
}
public static LocalDate addYear(int year){
return addYear(getNow(), year);
}
public static LocalDate addYear(LocalDate localDate, int year){
return localDate.plus(year, ChronoUnit.YEARS);
}
public static LocalTime subHours(int hours){
return addHours(getNowTime(), hours);
}
public static LocalTime subHours(LocalTime localTime, int hours){
return localTime.minusHours(hours);
}
public static LocalTime subMinutes(int minutes){
return subMinutes(getNowTime(), minutes);
}
public static LocalTime subMinutes(LocalTime localTime, int minutes){
return localTime.minusMinutes(minutes);
}
public static LocalTime subSeconds(int seconds){
return subSeconds(getNowTime(), seconds);
}
public static LocalTime subSeconds(LocalTime localTime, int seconds){
return localTime.minusSeconds(seconds);
}
public static LocalDate subWeek(int week){
return subWeek(getNow(), week);
}
public static LocalDate subWeek(LocalDate localDate, int week){
return localDate.minus(week, ChronoUnit.WEEKS);
}
//减n年
public static LocalDate subYear(int year){
return subYear(getNow(), year);
}
//减n年
public static LocalDate subYear(LocalDate localDate, int year){
return localDate.minus(year, ChronoUnit.YEARS);
}
//获取时间戳
public static long getNowTimeStamp() {
// 1、
// Returns the current time based on your system clock and set to UTC.
// Clock clock = Clock.systemUTC();
// System.out.println("Clock : " + clock.millis());
// 2、
// Returns time based on system clock zone
// Clock defaultClock = Clock.systemDefaultZone();
// defaultClock.millis();
// 3、
// 你也可以使用Date类和Instant类各自的转换方法互相转换,
// 例如:Date.from(Instant) 将Instant转换成java.util.Date,
// Date.toInstant()则是将Date类转换成Instant类。
Instant timestamp = Instant.now();
long l = timestamp.toEpochMilli();
return l;
}
//上海时区的时间
public static ZonedDateTime getLocalDateTimeByShanghai() {
// Date and time with timezone in Java 8
ZoneId america = ZoneId.of("Asia/Shanghai");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInShanghai = ZonedDateTime.of(localtDateAndTime, america );
return dateAndTimeInShanghai;
}
//这一个月有多少天
public static int getLengthOfMonth(int year, int month) {
YearMonth yearMonth = YearMonth.of(year, month);
return yearMonth.lengthOfMonth();
}
//这一年有多少天
public static int getLengthOfYear(int year) {
Year year1 = Year.of(year);
return year1.length();
}
//是否闰年
public static boolean isLeapYear(int year) {
Year year1 = Year.of(year);
return year1.isLeap();
}
public static int gapMonths(LocalDate date1, LocalDate date2){
Period periodToNextJavaRelease = Period.between(date1, date2);
return periodToNextJavaRelease.getMonths();
}
public static int gapDays(LocalDate date1, LocalDate date2){
Period periodToNextJavaRelease = Period.between(date1, date2);
return periodToNextJavaRelease.getDays();
}
public static Date localDate2Date(LocalDate date){
ZoneId zone = ZoneId.systemDefault();
Instant instant = date.atStartOfDay().atZone(zone).toInstant();
java.util.Date da = Date.from(instant);
return da;
}
public static LocalDate date2LocalDate(java.util.Date date){
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
LocalDate localDate= localDateTime.toLocalDate();
return localDate;
}
public static Date localDateTime2Date(LocalDateTime date){
ZoneId zone = ZoneId.systemDefault();
Instant instant = date.atZone(zone).toInstant();
java.util.Date da = Date.from(instant);
return da;
}
public static LocalDateTime date2LocalDateTime(java.util.Date date){
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime;
}
public static LocalDate format2LocalDate(String dayAfterTommorrow) {
//eg: String dayAfterTommorrow = "20180205";
LocalDate formatted = LocalDate.parse(dayAfterTommorrow,
DateTimeFormatter.BASIC_ISO_DATE);
return formatted;
}
public static void formatExample() {
LocalDateTime date = LocalDateTime.now();
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//日期转字符串
String str = date.format(format1);
System.out.println("日期转换为字符串:"+str);
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//字符串转日期
LocalDate date2 = LocalDate.parse(str,format2);
System.out.println("日期类型:"+date2);
}
}
边栏推荐
- Bulk copy baby upload prompt garbled, how to solve?
- ZJCTF_login
- Shortcut keys commonly used in idea
- 力扣(LeetCode)207. 课程表(2022.07.26)
- 5、 MFC view windows and documents
- Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
- Role of thread.sleep (0)
- [SQL简单题] LeetCode 627. 变更性别
- Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
- 毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了
猜你喜欢

Worth more than 100 million! The 86 version of "red boy" refuses to be a Daocheng Xueba. He is already a doctor of the Chinese Academy of Sciences and has 52 companies under his name

Baidu cloud face recognition

Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

CS224W fall 1.2 Applications of Graph ML

Hcip day 14 notes

周全的照护 解析LYRIQ锐歌电池安全设计

用最原始的方法纯手工实现常见的 20 个数组方法

Boom 3D new 2022 audio enhancement app

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
随机推荐
CS224W fall 课程 ---- 1.1 why Graphs ?
localStorage与sessionStorage
阿里云解决方案架构师张平:云原生数字化安全生产的体系建设
[dynamic planning medium] leetcode 198. looting 740. delete and get points
window对象的常见事件
Common events of window objects
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
An error in the fourth edition of the red book?
Compile and use protobuf Library in vs2019
[SQL简单题] LeetCode 627. 变更性别
用最原始的方法纯手工实现常见的 20 个数组方法
CS224W fall 1.2 Applications of Graph ML
Boom 3D全新2022版音频增强应用程序App
LabVIEW中编程更改进程的优先级
After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized
我的爬虫笔记(七) 通过爬虫实现blog访问量+1
Zhang Ping, Alibaba cloud Solution Architect: system construction of cloud native digital safety production
浅浅梳理一下双轴快排(DualPivotQuickSort)
[dynamic programming simple question] leetcode 53. maximum subarray and
Integrated water conservancy video monitoring station telemetry terminal video image water level water quality water quantity flow velocity monitoring