How To Format Date In Java?

SimpleDateFormat class of java.text package is used to format given Date object in different pattern. This class has 2 important methods – “parse()” and “format()”. To convert string to Date in java parse() method is used and  To format the Date object in desired patterns like dd-MM-yyyyyyyy/MM/dd hh:mm:ss etc format() method is used. While developing an application this program is needed to provide current date in desired format.

Pattern 1 : dd/MM/yyyy  

import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass
{
    public static void main(String[] args) 
    {
        //Getting today's date
        Date today = new Date();
        //Printing today's date in the default format
        System.out.println("Today is : "+today);

        //Formatting today's date in dd/MM/yyyy format

        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");     

        System.out.println("Today in dd/MM/yyyy format : "+formatter.format(today));
    }

}

Output :

Today is : Sun Oct. 22 18:19:39 IST 2017
Today in dd/MM/yyyy format : 22/10/2017

Pattern 2 :  yyyy-MM-dd

importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassMainClass
{
    publicstaticvoidmain(String[] args) 
    {
        //Getting today's date
        
        Date today = newDate();

        //Printing today's date in the default format

        System.out.println("Today is : "+today);

        //Formatting today's date in yyyy-MM-dd format

        SimpleDateFormat formatter = newSimpleDateFormat("yyyy-MM-dd");

        System.out.println("Today in yyyy-MM-dd format : "+formatter.format(today));
    }
}
Output :
Today is : Sun Oct 22 18:22:57 IST 2017
Today in yyyy-MM-dd format : 2017-10-22
Pattern 3 : dd MMMM yyyy
importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassMainClass
{

    publicstaticvoidmain(String[] args) 

    {
        //Getting today's date

        Date today = newDate();

        //Printing today's date in the default format

        System.out.println("Today is : "+today);

        //Formatting today's date in dd MMMM yyyy format

        SimpleDateFormat formatter = newSimpleDateFormat("dd MMMM yyyy");

        System.out.println("Today in dd MMMM yyyy format : "+formatter.format(today));
    }
}

Output :

Today is : Sun Oct 22 18:27:21 IST 2017
Today in dd MMMM yyyy format : 22 October 2017

Pattern 4 : dd MMM yyyy

importjava.text.SimpleDateFormat;
importjava.util.Date;
publicclassMainClass
{
    publicstaticvoidmain(String[] args)
    {
        //Getting today's date
        
        Date today = newDate();
        
        //Printing today's date in the default format
        
        System.out.println("Today is : "+today);
        
        //Formatting today's date in E, dd MMM yyyy format
        
        SimpleDateFormat formatter = newSimpleDateFormat("E, dd MMM yyyy");
        
        System.out.println("Today in E, dd MMM yyyy format : "+formatter.format(today));
    }
}
Output :
Today is : Sun Oct 22 18:34:05 IST 2017
Today in E, dd MMM yyyy format : Sun, 22 Oct 2017
Pattern 5 : dd-MMM-yyyy HH:mm:ss
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass
{
    public static void main(String[] args)
    {
        //Getting today's date
        
        Date today = new Date();
        
        //Printing today's date in the default format
        
        System.out.println("Today is : "+today);
        
        //Formatting today's date in dd-MMM-yyyy HH:mm:ss format
        
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
        
        System.out.println("Today in dd-MMM-yyyy HH:mm:ss format : "+formatter.format(today));
    }
}

Output :

Today is : Sun Oct 22 18:40:47 IST 2017
Today in dd-MMM-yyyy HH:mm:ss format : 22-Oct-2017 18:40:47

Pattern 6 : EEEE, MMM dd yyyy, hh:mm:ss

import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass
{
    public static void main(String[] args)
    {
        //Getting today's date
        
        Date today = new Date();
        
        //Printing today's date in the default format
        
        System.out.println("Today is : "+today);
        
        //Formatting today's date in EEEE, MMM dd yyyy, hh:mm:ss a format
        
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd yyyy, hh:mm:ss a");
        
        System.out.println("Today in EEEE, MMM dd yyyy, hh:mm:ss a format : "+formatter.format(today));
    }
}

Output :

Today is : Sun Oct 22 18:45:51 IST 2017
Today in EEEE, MMM dd yyyy, hh:mm:ss a format : Sunday, Oct 22 2017, 06:45:51 PM

Pattern 7 :  dd-MMM-yyyy HH:mm:ss

import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass
{
    public static void main(String[] args)
    {
        //Getting today's date
        
        Date today = new Date();
        
        //Printing today's date in the default format
        
        System.out.println("Today is : "+today);
        
        //Formatting today's date in dd-MMM-yyyy HH:mm:ss z format
        
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss z");
        
        System.out.println("Today in dd-MMM-yyyy HH:mm:ss z format : "+formatter.format(today));
    }
}

Output :

Today is : Sun Oct 22 18:49:53 IST 2017
Today in dd-MMM-yyyy HH:mm:ss z format : 22-Oct-2017 18:49:53 IST

Pattern 8 : dd-MMM-yyyy HH:mm:ss Z

import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass
{
    public static void main(String[] args)
    {
        //Getting today's date
        
        Date today = new Date();
        
        //Printing today's date in the default format
        
        System.out.println("Today is : "+today);
        
        //Formatting today's date in dd-MMM-yyyy HH:mm:ss Z format
        
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z");
        
        System.out.println("Today in dd-MMM-yyyy HH:mm:ss Z format : "+formatter.format(today));
    }
}

Output :

Today is : Sun Oct 22 19:01:39 IST 2017
Today in dd-MMM-yyyy HH:mm:ss Z format : 22-Oct-2017 19:01:39 +0530

Leave a Reply

avatar
  Subscribe  
Notify of