Skip to content

Java date format | String | Pattern with new Examples

  • by

Getting a date in Java programming is easy. Nowadays every application has needed to get a date in the database. When storing and retrieving a date in the database is very important to your code formatted date well without any error or with the thought of future perspective. You can do java date format in a string or any other pattern like DD-MM-YYYY.

How to Java date format String Pattern with new Examples

You will learn all about Java date format in this tutorial with an example and different ways.

Once you have data in any format you can do change its format in many ways like using a data DateFormat object or java.text.SimpleDateFormat class.

Java date Format Example

Here is an example of how to Java date format string using the SimpleDateFormat class. This class works on java.util.Date instances. Here are two examples, the first is string parsing and the second is with a new date(). A new date will show the current (Today) date.
And with string date using a “yyyy-mm-dd” pattern.

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

public class Hello {
    public static void main(String args[]) {
        // date string format
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dateFormat = format.parse("2019-01-31");
            System.out.println(dateFormat);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        // new date
        String dateNew = format.format( new Date());
        System.out.println(dateNew);
    }
}

Output: Thu Jan 31 00:00:00 IST 2019
2019-01-15

Not getting the Right Java date format pattern?

As above example, you might think where is string parse as “yyyy-mm-dd” pattern? You can get it in calendar date format as per the below example code.

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

public class Hello {
    public static void main(String args[]) {
        // date string format
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
        try {

            SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-DD");
            Date date = sDateFormat.parse("2019-01-31");

            SimpleDateFormat tDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            System.out.println(tDateFormat.format(date));

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output: 2019-01-31

Java calendar date format – Java 8

When you use to date, it contains date and time in a millisecond so when you printing a System.out.println(date) it will return this formatOutput “2019-05-12T17:11:53.678”.
In Java 8 following the below example, you can get the perfect date format.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Hello {
    public static void main(String args[]) {
        LocalDateTime ldt = LocalDateTime.now().plusDays(1);
        DateTimeFormatter formmat1 =
                DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
        System.out.println(ldt);

        String formatter = formmat1.format(ldt);
        System.out.println(formatter);

    }
}

Output: 2019-01-17T12:02:12.950
2019-01-17

Question: How to get the current date in java in dd mm yyyy format?

Answer: Simple just create a format and parses the date object as below example.

SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-DD");
String dateNew = format.format( new Date());

Question: How to convert date format in java?

Answer: As above examples using date or dateFormat or simpleDateFormat class instance you can convert your string to date object. There is many coding ways to do in java.

Side Note – Some important format

Here are some format you can use in parse a date and time. If you tried with another lettr like yyiy-mm-dd then it will throw an error.
java.lang.IllegalArgumentException: Illegal pattern character 'i'

Pattern FormatExample
YYYY-MM-DD (2019-12-31)
DD-MM-YYYY (31-12-2019)
yyyy-MM-dd HH:mm:ss 2019-12-31 23:59:59
HH:mm:ss.SSS 23:59.59.999
yyyy-MM-dd HH:mm:ss.SSS 2019-12-31 23:59:59.999
yyyy-MM-dd HH:mm:ss.SSS Z 2009-12-31 23:59:59.999 +0100

Format Letter List and Full form

If you don’t know the letter used in date format then following the below list will help you with this. Even caps letters are different output.

Letters  Full form
y year (yy or yyyy)
M month (MM)
d day in month (dd)
h hour (0-12) (hh)
H hour (0-23) (HH)
m minute in hour (mm)
s seconds (ss)
S milliseconds (SSS)
z time zone text (e.g. Pacific Standard Time…)
Z
time zone, time offset (e.g. -0800)

This tutorial totally based on how to get a date and parse it into a format. If you looking for something else on How to get a date in Java? or have another example then do comment below.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Examples Java date formatare in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *