package time;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class TestLocalDateTime {
private static final String TimeFormat="yyyy-MM-dd HH:mm:ss";
private static final String ZoneID="Asia/Shanghai";
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
//printTimeStr(now);
//parseDataTime(now);
LocalDateTime localDateTime = now.plusHours(1L);
//parseDataTime(localDateTime);
String s = printTimeStr(now);
parseTimeStr(s);
}
public static void test01() {
LocalDateTime now = LocalDateTime.now();
}
public static void test02() {
// 當前時間
LocalDateTime now = LocalDateTime.now();
// 5050年12月12日 14時30分 30秒 30納秒 (秒和納秒的部分可以省略)
LocalDateTime futureTime = LocalDateTime.of(5050, 12, 12, 14, 30, 30, 30);
// 添加: plus..() plusDays痴柔、plusMonth雾棺、plusYears...
LocalDateTime newTime_01 = now.plusDays(1); // 后一天
// 減少: minus..() minusDays凉夯、minusMonth狼牺、minusYears...
LocalDateTime newTime_02 = now.minusDays(1); // 前一天
// 設(shè)置各部分時間
LocalDateTime newYear = now.withYear(4040); // 改為4040年
LocalDateTime newMinute = now.withMinute(10); // 改為10月份
LocalDateTime newDay = now.withDayOfMonth(12); // 改為12日
// 通過字符串獲取
String timeString = "5050-12-12 14:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime parseTime = LocalDateTime.parse(timeString, formatter);
boolean isBefore = now.isBefore(newTime_01);
boolean isAfter = now.isAfter(newTime_01);
boolean isEqual = now.isEqual(newTime_01);
}
/**
* 將字符串時間轉(zhuǎn)換成時間戳
* */
public static void parseTimeStr(String timeStr) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeFormat);
System.out.println(LocalDateTime.parse(timeStr, dateTimeFormatter).atZone(ZoneId.of(ZoneID)).toInstant().getEpochSecond());
}
public static void test04() {
LocalDateTime now = LocalDateTime.now();
// get..() getHour、getMinute黑滴、getSecond...
int year = now.getYear();
Month month = now.getMonth(); // Month是枚舉璧帝,返回值為月份的英文大寫
int hour = now.getHour();
// 其余同理
}
public static String printTimeStr(LocalDateTime dateTime) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(TimeFormat);
String format = dateTimeFormatter.format(dateTime);
System.out.println( DateTimeFormatter.ofPattern(TimeFormat).format(LocalDateTime.now()));
System.out.println(format);
return format;
}
public static void test05() {
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 5050年12月12日 14時30分 30秒 30納秒 (秒和納秒的部分可以省略)
LocalDateTime futureTime = LocalDateTime.of(5050, 12, 12, 14, 30, 30, 30);
// 轉(zhuǎn)換為字符串
String timeString = format.format(futureTime);
// 字符串轉(zhuǎn)換為LocalDateTime
LocalDateTime parseTime = LocalDateTime.parse(timeString, format);
}
/**
* 將LocalDateTime轉(zhuǎn)成時間錯
* */
public static void parseDataTime(LocalDateTime dateTime) {
Instant instant = dateTime.atZone(ZoneId.of(ZoneID)).toInstant();
long epochSecond = instant.getEpochSecond();
System.out.println(epochSecond);
System.out.println(instant.toEpochMilli());
long now=System.currentTimeMillis();
System.out.println(now/1000);
System.out.println(now);
}
public static void test06() {
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zonedDateTime = now.atZone(ZoneId.of("Asia/Shanghai"));
Instant instant = zonedDateTime.toInstant();
long milli = instant.toEpochMilli(); // 以毫秒為單位的時間戳
long second = instant.getEpochSecond(); // 以秒為單位的時間戳
// 以 毫秒 為單位的時間戳轉(zhuǎn)為LocalDateTime
LocalDateTime timeByMilli = Instant.ofEpochMilli(milli).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
// 以 秒 為單位的時間戳轉(zhuǎn)為LocalDateTime
LocalDateTime timeBySecond = Instant.ofEpochSecond(second).atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime();
// LocalDate黄痪、LocalTime同理
}
public static void test07() {
}
}