Sunday, June 23, 2024
HomeJavaJava SimpleDateFormat Example

Java SimpleDateFormat Example

-

In Java SimpleDateFormat article we’ll see how to use java.text.SimpleDateFormat class, which provides methods to format date to string or parse string to date. The SimpleDateFormat is a class which inherits java.text.DateFormat class.

Creating a SimpleDateFormat Instance:

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

The pattern String parameter used for parsing and formatting of dates. The Pattern Examples are given at the end of the article.

Related Links:

Java Interview Questions

Formatting Dates:

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

String date = sdf.format(new Date());
System.out.println(date);

Output:

25-09-2017

Parsing Dates:

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

Date date = sdf.parse("25-09-2017");

Java SimpleDateFormat Example: Date to String

Let’s see an example to format date and time in java using java.text.SimpleDateFormat class.

import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.Locale;  
public class SimpleDateFormatDemo
{  
public static void main(String[] args)
 {  
    Date date = new Date();  
    SimpleDateFormat sdf= new SimpleDateFormat("MM/dd/yyyy");  
    String strDate = sdf.format(date);  
    System.out.println("Example for MM/dd/yyyy format: "+strDate);  
  
    sdf= new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");  
    strDate = sdf.format(date);  
    System.out.println("Example for dd-M-yyyy hh:mm:ss format: "+strDate);  
  
    sdf= new SimpleDateFormat("dd MMMM yyyy");  
    strDate = sdf.format(date);  
    System.out.println("Example for dd MMMM yyyy format: "+strDate);  
  
    sdf= new SimpleDateFormat("dd MMMM yyyy zzzz");  
    strDate = sdf.format(date);  
    System.out.println("Example for dd MMMM yyyy zzzz format: "+strDate);  
  
  }
}

Output:

Example for MM/dd/yyyy format: 09/25/2017
Example for dd-M-yyyy hh:mm:ss format: 25-09-2017 05:53:12
Example for dd MMMM yyyy format: 25 September 2017
Example for dd MMMM yyyy zzzz format: 25 September 2017 Coordinated Universal Time

Java SimpleDateFormat Example: String to Date

Let’s see the simple example to parse string into date using java.text.SimpleDateFormat class.

import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
public class SimpleDateFormatDemo2
{  
 public static void main(String[] args)
 {  
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");  
    try 
    {  
        Date d= sdf.parse("25/09/2017");  
        System.out.println("Date is: "+d);  
    } 
    catch (ParseException e) 
    {
        e.printStackTrace();
    }  
 }  
}

Output:

Date is: Mon Sep 25 00:00:00 UTC 2017

Pattern Examples:

Let’s look into few pattern examples:

Pattern Example
dd-MM-yy 25-09-17
dd-MM-yyyy 25-09-2017
MM-dd-yyyy 09-25-2017
yyyy-MM-dd 2017-09-25
yyyy-MM-dd HH:mm:ss 2017-09-25 18:07:24
yyyy-MM-dd HH:mm:ss.SSS 2017-09-25 18:08:33.627
yyyy-MM-dd HH:mm:ss.SSSZ 2017-09-25 18:09:11.827+0000
dd MMMM yyyy zzzz 25 September 2017 Coordinated Universal Time

References:

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

3 Sum | Leetcode #15 | 100 Days of Code Day 10

Today we are going to solve 3 Sum - our first Medium Level Array problem from Coderefer DSA Sheet. If you are new to this...

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Follow us

1,358FansLike
10FollowersFollow
397SubscribersSubscribe

Most Popular