Java
Timestamp
String Format
Programming
Date and Time Conversion

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

java
1import java.time.LocalDateTime;
2import java.time.format.DateTimeFormatter;
3
4public class TimestampExample {
5    public static void main(String[] args) {
6        LocalDateTime now = LocalDateTime.now();
7        DateTimeFormatter formatter =
8                DateTimeFormatter.ofPattern("yyyy.MM.dd.HH.mm.ss");
9
10        String formatted = now.format(formatter);
11        System.out.println(formatted);
12    }
13}

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:

java
1import java.time.ZoneId;
2import java.time.ZonedDateTime;
3import java.time.format.DateTimeFormatter;
4
5ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
6DateTimeFormatter formatter =
7        DateTimeFormatter.ofPattern("yyyy.MM.dd.HH.mm.ss");
8
9System.out.println(now.format(formatter));

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

  • yyyy four-digit year
  • MM month
  • dd day of month
  • HH hour in 24-hour format
  • mm minute
  • ss second

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:

java
String fileName = "backup-" + LocalDateTime.now().format(formatter) + ".zip";
System.out.println(fileName);

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:

java
1import java.text.SimpleDateFormat;
2import java.util.Date;
3
4SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
5String formatted = sdf.format(new Date());
6System.out.println(formatted);

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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms