当前位置:网站首页>【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
}
}
边栏推荐
- Two methods of implementing asynchronous calling function with QT
- Printing colored messages on the console with printf
- 你敢信?開發一個管理系統我只用了兩天
- Player actual combat 16 xdecode class
- [wechat applet] 6.1 applet configuration file
- Autofac初学(1)
- Array one
- Leetcode 2176. 统计数组中相等且可以被整除的数对
- Unhandled exception stack overflow
- 【MySQL】数据库基本操作
猜你喜欢

New technology: efficient self-monitoring visual pre training, local occlusion no longer need to worry!

Huawei equipment is configured with H virtual private network

Player practice 18 xresample

Soft test (VI) Chrome browser installation selenium IDE

面向优化科学研究领域的软件包

华为设备配置BGP AS号替换

sql跨库注入

我愿称之为史上最全的深度学习面经总结(附答案详解)

Autofac初学(1)

Redis核心配置和高级数据类型
随机推荐
程序分析与优化 - 6 循环优化
Postgresql14 installation and use tutorial
[wechat applet] 5 Applet structure directory
C magic skill Chapter 4 (detailed explanation of memory function)
[MySQL] basic database operation
Copy word content to excel and automatically divide it into multiple columns
Analysis of two-dimensional array passing as function parameter (C language)
Player actual combat 25 unpacking module add close
Player practice 20 audio thread and video thread
C secret arts script Chapter 2 (detailed explanation of pointer) (Section 2)
Leetcode 2185. 统计包含给定前缀的字符串
Raspberry pie get temperature and send pictures to email
String concatenation and ternary operators
Autofac初学(1)
Markdown edit
Leetcode 2176. 统计数组中相等且可以被整除的数对
Variable parameters
C secret script Chapter 1: data storage (in-depth analysis) supplement
你敢信?开发一个管理系统我只用了两天
工业机械臂(机器人)视觉定位引导系统