Get the current timestamp as a string in Java (yyyy.MM.dd.HH.mm.ss)
Codemia Knowledge Hub ·
Use java.time with a DateTimeFormatter: LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd.HH.mm.ss")). The two things to get right are the pattern letters and whether you want local time or a timezone-aware timestamp.
Formatting the local time with DateTimeFormatter
This is the standard solution for Java 8 and later. DateTimeFormatter is readable, immutable and thread-safe, which matters because timestamp formatting often lives in utility code used by many threads at once. You do not need the synchronization tricks older date APIs required.
Timezone-aware timestamps
LocalDateTime.now() uses the system default timezone but stores no timezone information in the value. That is fine for local logging or filenames, but a timestamp that looks right on one machine can mislead across systems in other zones. For a timezone-aware value, use ZonedDateTime or OffsetDateTime:
The pattern is the same, but the value now comes from a clearly defined timezone. That makes the code easier to reason about in distributed systems, log aggregation, or anywhere machines may run in different local timezones.
What each pattern letter means
yyyyfour-digit yearMMmonthddday of monthHHhour in 24-hour formatmmminutesssecond
The classic mistake is mixing up uppercase MM (month) and lowercase mm (minute). yyyy.mm.dd prints minutes where you meant the month. Similarly, use lowercase yyyy for the calendar year, not uppercase YYYY.
Using the timestamp in file names
The dot-separated format is popular for file names, logs and backup labels because it has no spaces or colons:
It is easy to read and generally filesystem-friendly. For storage or machine-facing APIs, though, ISO 8601 values often work better than a human-readable display format.
Legacy code with SimpleDateFormat
Older pre-Java-8 code may use:
It works, but SimpleDateFormat is mutable and not thread-safe, so prefer DateTimeFormatter in new code. If legacy code must keep using it, do not share one instance across threads unless you add explicit synchronization or thread-local handling.
Related reading
- How to get HTTP response code for a URL in Java?
- How to get InputStream via Spring-Feign?
- How to get kafka consume lag in java program
- How to get kubernetes service account access token using fabric8 java client?
- How to get local server host and port in Spring Boot?
- How to get milliseconds from LocalDateTime in Java 8
- How to get parameters from the URL with JSP
- How to get request URL in Spring Boot RestController
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.