Pattern Letters Decode Datetime Formats
Read ofPattern letter counts: M counts switch number to short, full, narrow text; yy parses with a 2000-2099 base; y is year-of-era while u is the year.
Code
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LogStampFormat {
public static void main(String[] args) {
LocalDateTime stamped = LocalDateTime.of(2026, 11, 23, 14, 5, 30, 978_000_000);
// M 的数量决定数字还是文本:1/2 位是数字,3/4/5 位是短/全/窄文本
System.out.println("M: " + stamped.format(DateTimeFormatter.ofPattern("M")));
System.out.println("MM: " + stamped.format(DateTimeFormatter.ofPattern("MM")));
System.out.println("MMM: " + stamped.format(DateTimeFormatter.ofPattern("MMM")));
System.out.println("MMMM: " + stamped.format(DateTimeFormatter.ofPattern("MMMM")));
System.out.println("MMMMM: " + stamped.format(DateTimeFormatter.ofPattern("MMMMM")));
// S 是秒的小数:SSS 即毫秒(截断纳秒到毫秒)
System.out.println("SSS: " + stamped.format(DateTimeFormatter.ofPattern("SSS")));
// yy 两位年份解析用 2000–2099 基准;y 是 year-of-era,u 是 proleptic year
LocalDate parsed = LocalDate.parse("26-11-23", DateTimeFormatter.ofPattern("yy-MM-dd"));
System.out.println("yy parse: " + parsed);
System.out.println("u vs y: "
+ stamped.format(DateTimeFormatter.ofPattern("u")) + " / "
+ stamped.format(DateTimeFormatter.ofPattern("yyyy")));
}
}Inside the snippet
Under the hood: the class LogStampFormat and the function main.
Watch out for
- Copy, run: the standard library answers every call in the file.
Putting it to use
The file’s steps show up in the output as M:, MM:, MMM:.
Core lines
LocalDate parsed = LocalDate.parse("26-11-23", DateTimeFormatter.ofPattern("yy-MM-dd"));
LocalDateTime stamped = LocalDateTime.of(2026, 11, 23, 14, 5, 30, 978_000_000);
+ stamped.format(DateTimeFormatter.ofPattern("u")) + " / "
+ stamped.format(DateTimeFormatter.ofPattern("yyyy")));
Interactive Tools for This Topic
Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.Indentation ConverterConvert leading indentation between tabs and spaces at a chosen width, leaving inner content untouched.
FAQ
- What does Pattern Letters Decode Datetime Formats print when run?
- Execute the whole file; each printed line is prefixed with a label like `M:`, `MM:`, `MMM:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.