hi 2 all
How to get todaz date in java and store in a string
thanx in advance

Dani AI

Generated

Regarding 's question about getting today's date in Java and storing it in a String: two concise, practical options follow. For modern code (Java 8+), prefer the java.time API. For older code, use java.util.Date + SimpleDateFormat but note the caveats.

Modern (Java 8+) — recommended

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

LocalDate today = LocalDate.now();                      // date only, system default zone
String isoDate = today.toString();                      // "yyyy-MM-dd" (ISO 8601)

DateTimeFormatter fmt = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String custom = today.format(fmt);                      // "19/11/2025"

LocalDateTime now = LocalDateTime.now();
String dateTime = now.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); // "yyyy-MM-ddTHH:mm:ss"

Notes: DateTimeFormatter and the java.time types are immutable and thread-safe. For a specific zone use ZonedDateTime.now(ZoneId.of("UTC")).

Legacy (pre-Java 8)

import java.util.Date;
import java.text.SimpleDateFormat;

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String s = sdf.format(now);

Caution: SimpleDateFormat is not thread-safe. In multi-threaded code, use ThreadLocal, synchronize access, or migrate to java.time.

Best practices and pitfalls

  • Prefer ISO 8601 (yyyy-MM-dd or yyyy-MM-dd'T'HH:mm:ssZ) for storage and interchange.
  • Use yyyy (calendar year), not YYYY (week-based year), to avoid surprises.
  • For persistence, store dates/times in typed database columns rather than plain strings when possible.

Official Java Date/Time docs and tutorial provide more patterns and details: Java Date/Time API (tutorial).

I am guessing that you havent even tried searching it for yourself.

So my advise would be to first search yourself and then if you have nay problems, then we will be happy to help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.