beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java SimpleDateFormat Class explained with examples

By Chaitanya Singh | Filed Under: Java Date

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

Leave a Reply Cancel reply

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

Java Date and Time

  • Java Date Time
  • Java LocalDate
  • Java LocalTime
  • Java LocalDateTime
  • Java ZonedDateTime
  • Java DateTimeFormatter
  • current date time

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap