当前位置:网站首页>【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
}
}
边栏推荐
- TestEngine with ID ‘junit-vintage‘ failed to discover tests
- Player actual combat 14 display YUV
- JD scanning code to obtain cookies
- [wechat applet] 6.1 applet configuration file
- Perfect ending | detailed explanation of the implementation principle of go Distributed Link Tracking
- What is automatic bidding? What are its advantages?
- QT multi thread drawing and real-time refreshing method
- 你敢信?開發一個管理系統我只用了兩天
- Detailed explanation of C language memset
- jenkins相关
猜你喜欢

The original Xiaoyuan personal blog project that has been around for a month is open source (the blog has basic functions, including background management)

TestEngine with ID ‘junit-vintage‘ failed to discover tests

JS (II) syntaxerror: cannot use import statement outside a module

Redis data deletion policy in 2022

Program analysis and Optimization - 6 loop optimization
![[OCR] aspriseocr C # English, number recognition (not Chinese)](/img/80/198145df663d2eeec6b8b1d7bc47b6.png)
[OCR] aspriseocr C # English, number recognition (not Chinese)

How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem

In C language, the main function calls another function, which is understood by assembly code

Analysis of two-dimensional array passing as function parameter (C language)

C secret arts script Chapter 5 (paragraph) (Section 3)
随机推荐
Can you believe it? It took me only two days to develop a management system
Basic usage of scanner
Appnium (II) installation and basic use of mitmproxy
Printing colored messages on the console with printf
完美收官|详解 Go 分布式链路追踪实现原理
【OCR】AspriseOCR C# 英文、数字识别(中文不行)
C secret arts script Chapter 2 (detailed explanation of pointers) (Section 3)
Leetcode 2176. Count equal and divisible pairs in an array
Copy word content to excel and automatically divide it into multiple columns
Visual positioning guidance system for industrial manipulator (robot)
Analysis of two-dimensional array passing as function parameter (C language)
C secret arts script Chapter 5 (structure) (Section 1)
Player practice 18 xresample
Postgresql14 installation and use tutorial
Unit test (I) unit test with JUnit
Simple code implementation of addition, subtraction, multiplication and division calculator
C secret arts script Chapter 5 (structure) (Section 2)
Notepad common settings
If you want to build brand awareness, what bidding strategy can you choose?
Tu oses le croire? Il m'a fallu deux jours pour développer un système de gestion.