How to get current timestamp in string format in Java? yyyy.MM.dd.HH.mm.ss
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In modern Java, the right way to get the current timestamp as a string such as yyyy.MM.dd.HH.mm.ss is to use java.time and DateTimeFormatter. The main things to get right are the format pattern and whether you want local time or a timezone-aware timestamp.
Use java.time First
For a local timestamp:
This is the standard Java 8 and later solution. It is readable, immutable, and thread-safe.
That combination matters because timestamp formatting often ends up in utility code used by many threads at once. With DateTimeFormatter, you do not need the synchronization tricks that older date APIs often required.
Choose the Right Time Type
LocalDateTime.now() uses the system default timezone but does not store timezone information in the value itself. That is fine for local logging or filenames, but not always enough for distributed systems.
If you want a timezone-aware timestamp, use ZonedDateTime or OffsetDateTime:
The output string uses the same pattern, but the value now comes from a clearly defined timezone.
That makes the code easier to reason about in distributed systems, log aggregation, or anything else where machines may not all run in the same local timezone.
Pattern Symbols Matter
The pattern yyyy.MM.dd.HH.mm.ss means:
- '
yyyyfour-digit year' - '
MMmonth' - '
ddday of month' - '
HHhour in 24-hour format' - '
mmminute' - '
sssecond'
The common mistake is confusing uppercase MM and lowercase mm. In Java:
- '
MMmeans month' - '
mmmeans minute'
So yyyy.mm.dd is wrong if you mean month. It prints minutes instead.
Use the Result for File Names Carefully
This format is often used in file names, logs, and backup labels because it avoids spaces and colons. That is one reason the dot-separated format remains popular:
It is easy to read and generally filesystem-friendly.
Legacy Code with SimpleDateFormat
If you are maintaining older pre-Java-8 code, you may still see:
This works, but SimpleDateFormat is mutable and not thread-safe. In new code, prefer DateTimeFormatter.
If legacy code must keep using it, avoid sharing a single formatter instance across threads unless you add explicit synchronization or thread-local handling.
Common Pitfalls
The biggest mistake is using the wrong pattern letters, especially lowercase mm for month or uppercase YYYY when you really want calendar year yyyy.
Another mistake is using local server time without thinking about timezone requirements. A timestamp that looks correct on one machine can be misleading across systems in other zones.
A third issue is choosing a display format for storage or APIs. Human-readable timestamps are fine for logs, but machine-facing interfaces often work better with ISO 8601 values.
Summary
- Use
LocalDateTimeandDateTimeFormatterfor local timestamps in modern Java. - Use
ZonedDateTimeorOffsetDateTimewhen timezone clarity matters. - The pattern
yyyy.MM.dd.HH.mm.ssuses uppercaseMMfor month and lowercasemmfor minute. - '
DateTimeFormatteris the preferred modern replacement forSimpleDateFormat.' - Think about timezone and output purpose, not just the string pattern.

