BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java SimpleDateFormat Class explained with examples

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

Java SimpleDateFormat class is used for formatting date and time. In the previous tutorial we have seen the examples of DateFormat class which is also used for the same purpose, the SimpleDateFormat class is a sub class of DateFormat class. In this guide, we will see how to format date and time using this class, along with the examples of methods of this class.

Java SimpleDateFormat Example – Formatting Date and time

In this example, we are formatting the current date and time using SimpleDateFormat class.

import java.text.SimpleDateFormat;  
import java.util.Date;   
public class Example {  
   public static void main(String[] args) { 
       //Getting the current date
       Date date = new Date();  
       //Specifying the format of the date using SimpleDateFormat
       SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy"); 
       //Formatting the date to the specified format
       String dateString = sdf.format(date);  
       System.out.println("Date in the format of MM-dd-yyyy : "+dateString);  
  
       sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");  
       dateString = sdf.format(date);  
       System.out.println("Date in the format of dd/MM/yyyy hh:mm:ss : "+dateString);  
  
       sdf = new SimpleDateFormat("dd, MMMM, yyyy");  
       dateString = sdf.format(date);  
       System.out.println("Date in the format of dd, MMMM, yyyy : "+dateString);  
  
       //Format with time zone
       sdf = new SimpleDateFormat("dd, MMMM, yyyy zzzz");  
       dateString = sdf.format(date);  
       System.out.println("Date in the format of dd, MMMM, yyyy zzzz : "+dateString);  
  
       //DateFormat day, date, time and time zone
       sdf = new SimpleDateFormat("E, dd/MMM/yyyy HH:mm:ss z");  
       dateString = sdf.format(date);  
       System.out.println("Date in the format of E, dd/MMM/yyyy HH:mm:ss z : "+dateString);  
    
       //DateFormat date and time zone
       sdf = new SimpleDateFormat("dd MMM yyyy z");  
       dateString = sdf.format(date);  
       System.out.println("Date in the format of dd MMM yyyy z : "+dateString); 
   }  
}

Output:

Date in the format of MM-dd-yyyy : 10-19-2017
Date in the format of dd/MM/yyyy hh:mm:ss : 19/10/2017 11:00:47
Date in the format of dd, MMMM, yyyy : 19, October, 2017
Date in the format of dd, MMMM, yyyy zzzz : 19, October, 2017 India Standard Time
Date in the format of E, dd/MMM/yyyy HH:mm:ss z : Thu, 19/Oct/2017 23:00:47 IST
Date in the format of dd MMM yyyy z : 19 Oct 2017 IST

SimpleDateFormat Example 2: Converting Date to String

In this example we are converting the current date to String using the format() method of SimpleDateFormat.

import java.text.SimpleDateFormat;  
import java.util.Date;  
public class Example {  
   public static void main(String[] args) {  
      Date date = new Date();  
      SimpleDateFormat sdf = new SimpleDateFormat("dd, MM, yyyy"); 
      //converting date to string using format() method
      String dateString = sdf.format(date);  
      System.out.println("Date is: "+dateString);  
   }  
}

Output:

Date is: 19, 10, 2017

Java SimpleDateFormat parse() method example

Using the parse() method of the SimpleDateFormat class to convert a given string to date in Java.

import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
public class Example {  
   public static void main(String[] args) {  
	//Specifying the format of the date
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");  
        try {  
      	   String dateString = "19-06-2017";
    	   //String to date conversion
           Date date = sdf.parse(dateString);  
           System.out.println(date);  
        } catch (ParseException e) {
    	     e.printStackTrace();
    	  }  
   }  
}

Output:

Mon Jun 19 00:00:00 IST 2017

Reference:

SimpleDateFormat – JavaDoc

Related Posts:

  1. Java – DateFormat Class
  2. Java – Parse Date Example
  3. Java – Compare Dates
  4. Java – Date Formatting with TimeZone
  5. Java – Calculate Days between two Days

Top Related Articles:

  1. Convert String to date in Java
  2. Java Date Validation Example
  3. Java Access Modifiers – Public, Private, Protected & Default
  4. Date Formatting In Java With Time Zone
  5. Compare two dates with each other in Java

Tags: Java-Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap