Tutorials

Java Date Format


Java DateFormat


To format and parse date, java provides us with:

  1. DateFormat
  2. SimpleDateFormat

Formatting means converting Date into String.

Parsing means converting String into Date.

 Java DateFormat

Java DateFormat is an abstract class. It extends java.text.Format class. SimpleDateFormat extends DateFormat class.

This class provides various methods to parse and format date and time in java.

Parsing example:

 

Formatting example


package Date;

import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample {
 
      public static void main(String[] args) { 

           

            Date currentDate = new Date(); 
            System.out.println("Current Date: "+currentDate);          

            String dateToString = DateFormat.getInstance().format(currentDate); 
            System.out.println("Date Format: "+dateToString); 
      } 
}

OUTPUT:

Current Date: Thu Jun 07 11:36:49 IST 2018
Date Format: 6/7/18 11:36 AM

 

Parsing Example

 

package Date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateParseExample {

      public static void main(String[] args)throws Exception {
           
            DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            Date date = format.parse("07/06/2015"); 
            System.out.println("Date is: "+date); 
      } 
}

OUTPUT:

Date : Sun Jun 07 00:00:00 IST 2015

 



Please Share this page
Views : 136
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!