当前位置:网站首页>【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th
【LocalDate LocalTime LocalDateTime】1. Using immutability to achieve thread safety 2 Current date, current time, current date time 3 Since the time zone is not considered, you need to add 8 hours to th
2022-06-12 14:36:00 【Understand the principle + good code skills】
1) test 1
package org.example.testTime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
public class TestLocalDateTime {
public static void main(String[] args) {
// step1. date
LocalDate now = LocalDate.now();
System.out.println("now=" + now); // now=2022-02-18
// step2. Time
LocalTime now1 = LocalTime.now();
System.out.println("now1=" + now1.withNano(0)); // now1=00:45:19
// step3. date + Time
LocalDateTime now2 = LocalDateTime.now();
System.out.println("now2=" + now2.withNano(0)); // now2=2022-02-18T00:46:04
// step4. Specify month, day, hour, minute and second
LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8, 0);
System.out.println("of=" + of); // of=2008-08-08T20:08:08
// step5. Designated year
LocalDateTime of2 = of.withYear(2022);
System.out.println("of2=" + of2); // of2=2022-08-08T20:08:08
// step6. Add a day
LocalDateTime of3 = of2.plusDays(2);
System.out.println("of3=" + of3); // of3=2022-08-10T20:08:08
// step7. Specify the timestamp of the day of the week
int dayOfWeek = 7;
LocalDateTime ldt = LocalDateTime.now();
LocalDateTime newTime = ldt.minusDays(ldt.getDayOfWeek().getValue()) // Let's put the day of the week into 0
.plusDays(dayOfWeek) // Plus the number of days elapsed
.withHour(0)
.withMinute(0)
.withSecond(0)
.withNano(0);
System.out.println("newTime=" + newTime); // newTime=2022-02-20T00:00
// step8. Get the current timestamp ( millisecond )
LocalDateTime now3 = LocalDateTime.now();
long l = now3.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println("l=" + l); // l=1645116890691
}
}
2) test 2
TimeService.java
package org.example.testTime.service;
import org.example.testTime.logic.time.TimeManager;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class TimeService {
/**
* Zhou {} {}:{}:{} The timestamp
*
* @param str Such as : "05:00:00"
* @param loop Such as : 7
* @return
*/
public static long getSpecificDayTime(String str, int loop) {
String[] split = str.split(":");
int h = Integer.parseInt(split[0]);
int min = Integer.parseInt(split[1]);
int sec = Integer.parseInt(split[2]);
LocalDateTime weekDay = TimeManager.getSpecialWeekDayAtStartOfDayTime(loop);
LocalDateTime time = weekDay
.withHour(h)
.withMinute(min)
.withSecond(sec)
.withNano(0);
return TimeManager.localDateTimeToTimeMilli(time);
}
/**
* 2 Whether the timestamps are on the same day
*
* @param timestamp1
* @param timestamp2
* @return
*/
public static boolean isSameDay(long timestamp1, long timestamp2) {
LocalDate localDate1 = TimeManager.getLocalDateByTimestamp(timestamp1);
LocalDate localDate2 = TimeManager.getLocalDateByTimestamp(timestamp2);
return localDate1.equals(localDate2);
}
/**
* Current timestamp
*
* @return
*/
public static long getCurrentTimeMillis() {
return TimeManager.localDateTimeToTimeMilli(LocalDateTime.now());
}
}
TimeManager.java
package org.example.testTime.logic.time;
import java.time.*;
public class TimeManager {
private static final ZoneId ZONE_ID = ZoneId.systemDefault();
/**
* Timestamp of current time
*
* @param time
* @return
*/
public static long localDateTimeToTimeMilli(LocalDateTime time) {
return time.atZone(ZONE_ID).toInstant().toEpochMilli();
}
/**
* Zhou {} 0 Local time of point
*
* @param loop
* @return
*/
public static LocalDateTime getSpecialWeekDayAtStartOfDayTime(int loop) {
DayOfWeek dayOfWeek = DayOfWeek.of(loop);
// Get today's zero
LocalDateTime now = LocalDate.now().atStartOfDay();
// According to the week shift, we get
LocalDateTime weekDay = now.minusDays(now.getDayOfWeek().getValue() - dayOfWeek.getValue());
return weekDay;
}
/**
* Time stamp to date time
*
* @param timestamp
* @return
*/
public static LocalDateTime getLocalDateTimeByTimestamp(long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZONE_ID);
}
/**
* Time stamp date
*
* @param timestamp
* @return
*/
public static LocalDate getLocalDateByTimestamp(long timestamp) {
return getLocalDateTimeByTimestamp(timestamp).toLocalDate();
}
}
Main.java
package org.example.testTime;
import org.example.testTime.service.TimeService;
public class Main {
public static void main(String[] args) {
// step1: Test specified week {} {}:{}:{} The timestamp
System.out.println(TimeService.getSpecificDayTime("00:00:00", 7)); // 1645286400000 ==>2022-02-20 00:00:00
System.out.println(TimeService.getSpecificDayTime("05:00:00", 7)); // 1645304400000 ==>2022-02-20 05:00:00
// step2: Judge 2 Whether the timestamps are the same day
System.out.println(TimeService.isSameDay(1645286400000L, 1645304400000L));
// step3. Current timestamp
System.out.println(System.currentTimeMillis()); // 1645168940559
System.out.println(TimeService.getCurrentTimeMillis()); // 1645168940559
}
}
边栏推荐
- In C language, the main function calls another function, which is understood by assembly code
- MAT的安装和使用
- Socket model of punctual atom stm32f429 core board
- webdriver入门
- Reverse order of Excel
- sql跨库注入
- Player practice 15 xdemux and avcodecparameters
- How to set, reset and reverse bit
- How test engineers transform test and open
- Redis data deletion policy in 2022
猜你喜欢

你敢信?開發一個管理系統我只用了兩天

Player practice 26 adding slider and window maximization

Pay attention to click and pursue more users to enter the website. What bidding strategy can you choose?

For cross-border e-commerce, the bidding strategy focusing more on revenue - Google SEM

华为设备配置OSPF伪连接

Configuring OSPF pseudo connection for Huawei devices

Leetcode 2176. Count equal and divisible pairs in an array

Redis data deletion policy in 2022

Soft test (VI) Chrome browser installation selenium IDE

PMP agile knowledge points
随机推荐
Notepad common settings
Postgresql14 installation and use tutorial
Crawler problem set (I) record
C语言中主函数调用另外一个函数,汇编代码理解
Player practice 17 xvideowidget
[MySQL] basic database operation
JS (I) error [err\u module\u not\u found]: cannot find package 'UUID' imported
Junit多线程的写法
MobileOne: 移动端仅需1ms的高性能骨干,你值得拥有!
Jetpack架构组件学习(3)——Activity Results API使用
Tu oses le croire? Il m'a fallu deux jours pour développer un système de gestion.
Mémoire de l'examen d'entrée à l'université
Unit test (I) unit test with JUnit
Printing colored messages on the console with printf
Huawei equipment configuration BGP as number replacement
recursive learning
JD scanning code to obtain cookies
win10_ Home Edition cannot use remote desktop, and can be accessed by remote desktop.
In C language, the main function calls another function, which is understood by assembly code
Common DOS commands