Java SimpleDateFormat Example

0
60
java simpledateformat

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

avatar

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

  Subscribe  
Notify of