季度字符串转LocalDate
// 方式一
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral("-Q")
.appendValue(IsoFields.QUARTER_OF_YEAR, 1)
.appendLiteral("-")
.appendValue(IsoFields.DAY_OF_QUARTER,2)
.toFormatter(Locale.CHINA);
LocalDate parse = LocalDate.parse("2023-Q2-73", formatter);
System.out.println("parse = " + parse); //parse = 2023-06-12
// 方式二
String s = "2023-Q2";
LocalDate start = LocalDate.of(
Integer.parseInt(s.substring(0, 4)),
Integer.parseInt(s.charAt(6)+"") * 3 - 2, 1);
LocalDate end = LocalDate.of(
Integer.parseInt(s.substring(0, 4)), Integer.parseInt(s.charAt(6)+"") * 3, 1)
.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("start = " + start + "; end = " + end); // start = 2023-04-01; end = 2023-06-30
LocalDate转为季度字符串
//方式一
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral("-Q")
.appendValue(IsoFields.QUARTER_OF_YEAR, 1)
.toFormatter(Locale.CHINA);
String format = LocalDate.now().format(formatter);
System.out.println("format = " + format); // format = 2023-Q2
//方式二
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-qq");
String format1 = LocalDate.now().format(pattern);
System.out.println("format = " + format); // format = 2023-02
//方式三
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-qqq");
String format = LocalDate.now().format(pattern);
System.out.println("format = " + format); //format = 2023-2季度
//方式四
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-qqqq");
String format = LocalDate.now().format(pattern);
System.out.println("format = " + format); //format = 2023-第二季度
//方式五
DateTimeFormatter formatter = builder.appendValue(ChronoField.YEAR_OF_ERA, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral("-Q")
.appendValue(IsoFields.QUARTER_OF_YEAR, 1)
.appendLiteral("-")
.appendValue(IsoFields.DAY_OF_QUARTER,2)
.toFormatter(Locale.CHINA);
String format = LocalDate.now().format(formatter);
System.out.println("format = " + format); //format = 2023-Q2-73 二季度第73天
标签:format,appendValue,使用,季度,System,DateTimeFormatter,2023,LocalDate
From: https://www.cnblogs.com/walkAlwaysInCode/p/17472861.html