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.
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 2026may 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.Datejava.text.DateFormatjava.text.SimpleDateFormatjava.util.Calendar
What is Date Class?
The Date class in java.util represents a specific instant in time.
Example
Output may look like:
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
This prints the current date in short format.
Different DateFormat Styles
Depending on locale, output may look like:
Formatting Time Using DateFormat
Java can also format only the time portion.
Date and Time Together
We can also format both date and time together using:
Example
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
Output:
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
2. Year/Month/Day
3. Full Date with Day Name
4. Date with Time
Formatting Current Date and Time
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
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
Wrong Example
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
Creating a Specific Date Using Calendar
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.
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
2. Creating a Timestamp for Logs
3. Parsing User Input Date
Lenient Parsing
By default, SimpleDateFormat can be lenient, meaning it may adjust invalid dates automatically.
Example:
This may not fail immediately because Java tries to adjust the date.
To avoid this, turn leniency off:
This makes parsing stricter and safer.
Common Mistakes in Date Formatting
- using
mmfor month instead ofMM - using
hhinstead ofHHin 24-hour time - pattern not matching input during parsing
- forgetting locale when displaying month/day names for international users
- not handling parsing exceptions properly
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
DateFormatis an abstract class used for formatting and parsing datesSimpleDateFormatis used for custom date-time patternsformat()convertsDateto stringparse()converts string toDateMMmeans month,mmmeans minutesyyyyis 4-digit year,yyis 2-digit yearhhis 12-hour format,HHis 24-hour formatSimpleDateFormatis 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.
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.

