当前位置: 代码迷 >> 综合 >> LocalTime LocalDate LocalDateTime使用需求
  详细解决方案

LocalTime LocalDate LocalDateTime使用需求

热度:96   发布时间:2024-02-21 00:31:10.0

LocalTime LocalDate LocalDateTime使用需求


import org.junit.Test;import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;/*** @author zhangmingkun* @Description:* @Data: 2020/9/17 19:32*/
public class MainTest {//格式化时间字符串public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");public static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM");public static final DateTimeFormatter SHORT_DATE_FORMATTER = DateTimeFormatter.ofPattern("yy-MM-dd");public static final DateTimeFormatter SHORT_DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yy-MM-dd HH:mm:ss");public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");@Testpublic void testThread() {LocalDate today = LocalDate.now();System.out.println(today);//根据LocalDate获取最近一星期的时间和星期表示List<String> thisWeek = new ArrayList<>(7);for (int i = -1; i < 6; i++) {if (i == 0) {thisWeek.add("今天");continue;}thisWeek.add(convertToWeekName(today.getDayOfWeek().plus(i).toString()));}System.out.println(thisWeek.toString());LocalDateTime now = LocalDateTime.now();System.out.println(now.format(SHORT_DATETIME_FORMATTER));System.out.println(now.format(SHORT_DATE_FORMATTER));System.out.println(now.format(TIME_FORMATTER));System.out.println(now.format(MONTH_FORMATTER));System.out.println(now.format(DATETIME_FORMATTER));System.out.println(now.format(DATE_FORMATTER));// 取2017年1月第一个周一,用Calendar要死掉很多脑细胞:LocalDate firstMondayOf2017 = LocalDate.parse("2017-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2017-01-02System.out.println(firstMondayOf2017);//根据LocalDate获取10位时间戳Long firstMill = today.atStartOfDay(ZoneOffset.ofHours(8)).toEpochSecond();Long endMill = today.plusDays(1).atStartOfDay(ZoneOffset.ofHours(8)).toEpochSecond();System.out.println(firstMill);System.out.println(endMill);//根据LocalDate获取13位时间戳Long firstMill13 = today.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();Long endMill13 = today.plusDays(1).atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();System.out.println(firstMill13);System.out.println(endMill13);}private static String convertToWeekName(String name) {if (StringUtils.isEmpty(name)) {return null;}String weekName;switch (name) {case "MONDAY":weekName = "星期一";break;case "TUESDAY":weekName = "星期二";break;case "WEDNESDAY":weekName = "星期三";break;case "THURSDAY":weekName = "星期四";break;case "FRIDAY":weekName = "星期五";break;case "SATURDAY":weekName = "星期六";break;case "SUNDAY":weekName = "星期日";break;default:throw new IllegalStateException("Unexpected value convert to WeekName: " + name);}return weekName;}}

2020-09-21
[星期日, 今天, 星期二, 星期三, 星期四, 星期五, 星期六]
20-09-21 17:52:36
20-09-21
17:52:36
2020-09
2020-09-21 17:52:36
2020-09-21
2017-01-02
1600617600
1600704000
1600617600000
1600704000000

  相关解决方案