Java
Timestamp
String Format
Programming
Date and Time Conversion

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:

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 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:

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 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:

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

The common mistake is confusing uppercase MM and lowercase mm. In Java:

  • 'MM means month'
  • 'mm means 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:

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

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:

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);

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 LocalDateTime and DateTimeFormatter for local timestamps in modern Java.
  • Use ZonedDateTime or OffsetDateTime when timezone clarity matters.
  • The pattern yyyy.MM.dd.HH.mm.ss uses uppercase MM for month and lowercase mm for minute.
  • 'DateTimeFormatter is the preferred modern replacement for SimpleDateFormat.'
  • Think about timezone and output purpose, not just the string pattern.

Course illustration
Course illustration

All Rights Reserved.