Formatting date, time (java.text package)

Java 9 min min read Updated: Mar 31, 2026 Beginner
Formatting date, time (java.text package)
Beginner Topic 14 of 14

Formatting Date and Time (java.text Package)

In Java applications, date and time values are used almost everywhere. For example, we may need to display a user’s registration date, print invoice timestamps, format logs, store order time, or show a friendly human-readable date on screen. Raw date objects are not always suitable for direct display, so Java provides formatting tools to convert them into meaningful string formats.

Before the modern Java 8 Date-Time API became popular, Java mainly used classes from the java.util and java.text packages for date and time handling and formatting. One of the most important classes in this area is SimpleDateFormat.

Key Concept: Date and time formatting in older Java is mainly done using the java.text package, especially DateFormat and SimpleDateFormat, which convert date objects into formatted strings and parse strings back into date objects.

Why Date and Time Formatting is Needed

A date object internally stores date-time information, but users usually need it in a readable format.

For example:

  • Tue Apr 01 10:45:12 IST 2026 may not be ideal for UI display
  • we may want 01-04-2026
  • or 01 Apr 2026
  • or 2026/04/01 10:45 AM

Formatting allows developers to control how date and time appear.

Main Packages Used

Traditional date-time formatting generally uses:

  • java.util.Date
  • java.text.DateFormat
  • java.text.SimpleDateFormat
  • java.util.Calendar

What is Date Class?

The Date class in java.util represents a specific instant in time.

Example

java import java.util.Date; public class Main { public static void main(String[] args) { Date date = new Date(); System.out.println(date); } }

Output may look like:

text Tue Apr 01 10:45:12 IST 2026

This format is automatically generated but may not match application requirements.

What is DateFormat?

DateFormat is an abstract class in java.text used for formatting and parsing dates.

It provides predefined formatting styles such as:

  • SHORT
  • MEDIUM
  • LONG
  • FULL

Formatting Date Using DateFormat

java import java.util.Date; import java.text.DateFormat; public class Main { public static void main(String[] args) { Date date = new Date(); DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); System.out.println(df.format(date)); } }

This prints the current date in short format.

Different DateFormat Styles

java import java.util.Date; import java.text.DateFormat; public class Main { public static void main(String[] args) { Date date = new Date(); System.out.println(DateFormat.getDateInstance(DateFormat.SHORT).format(date)); System.out.println(DateFormat.getDateInstance(DateFormat.MEDIUM).format(date)); System.out.println(DateFormat.getDateInstance(DateFormat.LONG).format(date)); System.out.println(DateFormat.getDateInstance(DateFormat.FULL).format(date)); } }

Depending on locale, output may look like:

text 01/04/26 01-Apr-2026 01 April 2026 Tuesday, 01 April 2026

Formatting Time Using DateFormat

Java can also format only the time portion.

java import java.util.Date; import java.text.DateFormat; public class Main { public static void main(String[] args) { Date date = new Date(); System.out.println(DateFormat.getTimeInstance(DateFormat.SHORT).format(date)); System.out.println(DateFormat.getTimeInstance(DateFormat.MEDIUM).format(date)); System.out.println(DateFormat.getTimeInstance(DateFormat.LONG).format(date)); } }

Date and Time Together

We can also format both date and time together using:

java DateFormat.getDateTimeInstance(...)

Example

java import java.util.Date; import java.text.DateFormat; public class Main { public static void main(String[] args) { Date date = new Date(); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT); System.out.println(df.format(date)); } }

What is SimpleDateFormat?

SimpleDateFormat is one of the most important classes in the java.text package. It allows custom formatting of date and time using pattern symbols.

This gives developers much more control compared to predefined styles.

Basic SimpleDateFormat Example

java import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); System.out.println(sdf.format(date)); } }

Output:

text 01-04-2026

Common Pattern Symbols in SimpleDateFormat

Pattern Meaning Example
dd day of month 01
MM month number 04
MMM short month name Apr
MMMM full month name April
yy 2-digit year 26
yyyy 4-digit year 2026
HH hour in 24-hour format 16
hh hour in 12-hour format 04
mm minutes 45
ss seconds 12
a AM/PM PM
E day name Tue
EEEE full day name Tuesday

Examples of Custom Date Formats

1. Day-Month-Year

java SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

2. Year/Month/Day

java SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");

3. Full Date with Day Name

java SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd MMMM yyyy");

4. Date with Time

java SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");

Formatting Current Date and Time

java import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) { Date now = new Date(); SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm:ss a"); SimpleDateFormat sdf3 = new SimpleDateFormat("EEEE, dd MMM yyyy hh:mm:ss a"); System.out.println(sdf1.format(now)); System.out.println(sdf2.format(now)); System.out.println(sdf3.format(now)); } }

Parsing String into Date

Formatting converts a Date object into a string. Parsing does the reverse: it converts a string into a Date object.

Example

java import java.util.Date; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) throws Exception { String dateStr = "01-04-2026"; SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); Date date = sdf.parse(dateStr); System.out.println(date); } }

Here, the string is converted into a Java Date object.

Important Rule in Parsing

The format pattern must match the input string exactly. Otherwise, parsing may fail or behave unexpectedly.

Correct Example

java String input = "15/04/2026"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Wrong Example

java String input = "15/04/2026"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

The pattern does not match the input format.

Formatting Date Using Calendar

The Calendar class can also be used along with formatting classes. It provides more control for manipulating date-time values.

Example

java import java.util.Calendar; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a"); System.out.println(sdf.format(cal.getTime())); } }

Creating a Specific Date Using Calendar

java import java.util.Calendar; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(2026, Calendar.APRIL, 1); SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); System.out.println(sdf.format(cal.getTime())); } }

This is useful when building dates manually.

Formatting with Locale

Date formatting can depend on locale. Different regions may display date and month names differently.

java import java.util.Date; import java.util.Locale; import java.text.SimpleDateFormat; public class Main { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd MMMM yyyy", Locale.US); System.out.println(sdf.format(date)); } }

Locale is useful for international applications.

Common Formatting Patterns

Format Pattern
01-04-2026 dd-MM-yyyy
2026/04/01 yyyy/MM/dd
01 Apr 2026 dd MMM yyyy
Wednesday, 01 April 2026 EEEE, dd MMMM yyyy
10:45 PM hh:mm a
22:45:12 HH:mm:ss

Use Cases in Real Applications

1. Displaying User Registration Date

java Date regDate = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy"); System.out.println("Registered on: " + sdf.format(regDate));

2. Creating a Timestamp for Logs

java Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Log Time: " + sdf.format(now));

3. Parsing User Input Date

java String input = "25-12-2026"; SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); Date d = sdf.parse(input); System.out.println(d);

Lenient Parsing

By default, SimpleDateFormat can be lenient, meaning it may adjust invalid dates automatically.

Example:

java SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); Date d = sdf.parse("35-04-2026");

This may not fail immediately because Java tries to adjust the date.

To avoid this, turn leniency off:

java SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); sdf.setLenient(false);

This makes parsing stricter and safer.

Common Mistakes in Date Formatting

  • using mm for month instead of MM
  • using hh instead of HH in 24-hour time
  • pattern not matching input during parsing
  • forgetting locale when displaying month/day names for international users
  • not handling parsing exceptions properly
Important: In date patterns, MM means month and mm means minutes. This is one of the most common beginner mistakes.

Best Practices

  • use clear formatting patterns based on use case
  • match parsing pattern exactly with input format
  • handle parsing errors with try-catch
  • use strict parsing when validating user date input
  • choose readable output for UI and machine-friendly output for logs

Difference Between Formatting and Parsing

Operation Meaning
Formatting Converts Date object into string
Parsing Converts string into Date object

Interview-Oriented Points

  • DateFormat is an abstract class used for formatting and parsing dates
  • SimpleDateFormat is used for custom date-time patterns
  • format() converts Date to string
  • parse() converts string to Date
  • MM means month, mm means minutes
  • yyyy is 4-digit year, yy is 2-digit year
  • hh is 12-hour format, HH is 24-hour format
  • SimpleDateFormat is widely used in older Java code bases

Conclusion

Formatting date and time is an essential part of Java programming because date values must often be displayed or read in human-friendly formats. The java.text package, especially DateFormat and SimpleDateFormat, provides powerful tools for formatting and parsing dates in older Java applications.

A strong understanding of date pattern symbols, formatting rules, and parsing logic helps developers avoid common errors and write more professional Java applications, especially when working with forms, reports, logs, and user-facing data.

Quick Summary: The java.text package helps format and parse date/time values, and SimpleDateFormat is the most commonly used class for custom date and time patterns in older Java.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators