Skip to content

Java SimpleDateFormat

Imagine this: you've got a date in your Java code, but it's all messy and confusing. Maybe it looks like "2024-04-24T15:30:00". Not exactly easy on the eyes, right?

Simply put, SimpleDateFormat is like a magic for formatting dates and times just the way you want them. Whether you need to display dates in a specific format, parse dates from strings, or even localize them for different languages and regions, this little gem has got your back.

With SimpleDateFormat, you can transform that jumble of numbers and letters into something much nicer, like "April 24, 2024, 3:30 PM". Ah, much better!

But here's the best part: it's not rocket science. Even if you're new to programming, you can wrap your head around SimpleDateFormat with a bit of practice.

Imagine you have a Java program where you need to display the current date in a nice, readable format. Here's how you can do it using SimpleDateFormat:

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

public class Main {
    public static void main(String[] args) {
        // Create a SimpleDateFormat object with the desired date format
        SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy 'at' hh:mm a");

        // Get the current date and time
        Date currentDate = new Date();

        // Format the date using SimpleDateFormat
        String formattedDate = dateFormat.format(currentDate);

        // Print the formatted date
        System.out.println("Today's date is: " + formattedDate);

Now, let's break it down:

  • We import the SimpleDateFormat and Date classes from the java.text and java.util packages, respectively.
  • Inside the main method, we create a SimpleDateFormat object called dateFormat and specify the desired date format using a pattern. In this case, "MMMM dd, yyyy 'at' hh:mm a" will format the date like "April 24, 2024 at 03:30 PM".
  • We then create a Date object called currentDate and initialize it with the current date and time.
  • Using dateFormat.format(currentDate), we format the currentDate according to the pattern specified earlier and store it in the formattedDate string.
  • Finally, we print out the formatted date using System.out.println().

When you run this program, you'll see something like:

Today's date is: April 24, 2024 at 03:30 PM

Pretty neat, right? With SimpleDateFormat, you can easily customize date formats to suit your needs and make your Java applications more user-friendly. So go ahead, experiment with different patterns, and have fun mastering the art of date formatting!

Java Date Format Pattern Syntax

yYear (numeric, minimum digits)2024, 24
yyYear (numeric, 2 digits)24
yyyYear (numeric, minimum digits, with zero)2024, 024
yyyyYear (numeric, 4 digits)2024
MMonth in year (numeric, no padding)4
MMMonth in year (numeric, 2 digits, zero-padded)04
MMMMonth in year (abbreviated name)Apr
MMMMMonth in year (full name)April
dDay in month (numeric, no padding)7
ddDay in month (numeric, 2 digits, zero-padded)07
EDay in week (abbreviated name)Sun
EEEEDay in week (full name)Sunday
HHour in day (0-23, no padding)7
HHHour in day (0-23, 2 digits, zero-padded)07
hHour in am/pm (1-12, no padding)5
hhHour in am/pm (1-12, 2 digits, zero-padded)05
aAM/PM markerAM, PM
mMinute in hour (no padding)5
mmMinute in hour (2 digits, zero-padded)05
sSecond in minute (no padding)8
ssSecond in minute (2 digits, zero-padded)08
SMillisecond (1 digit)7
SSMillisecond (2 digits, zero-padded)07
SSSMillisecond (3 digits, zero-padded)007
zTime zone (short)IST
zzzzTime zone (full name)India Standard Time
'text'Literal text (enclosed in single quotes)'at', 'on', 'in'


Pattern: "yyyy-MM-dd HH:mm:ss" Date: 2024-04-24 15:30:45

In this example:

  • yyyy: Represents the year as "2024".
  • MM: Represents the month as "04" (April).
  • dd: Represents the day as "24".
  • HH: Represents the hour in 24-hour format as "15".
  • mm: Represents the minute as "30".
  • ss: Represents the second as "45".

You can combine these symbols in any order and include additional text within single quotes to represent the desired format for your date.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.