Thursday, June 1, 2023
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

Packages in Java

Hello World! Today's blog is about packages in java. Here, we will learn about what are packages and how we can use packages along with...

House Robber Problem | Maximum Sum of non-adjacent elements

In this blog post, we will discuss about how to find maximum sum of Non-Adjacent elements. Since House Robber Problem is a typical DP Problem...

Linked List Insertion – Linked List Tutorial Series #2

Linked List Insertion, unlike array or list insertion, is not pretty straight forward. In this article, we will discuss about Singly Linked List insertion. Usually...

Linked List Series #1 – Introduction – Creating a Linked list

Linked List, like arrays, is an example of Linear data structure. While linked lists are not a popular day to day data structures, they have...

Follow us

1,358FansLike
10FollowersFollow
402SubscribersSubscribe

Most Popular