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 Date Format examples

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

Java DateFormat class is an abstract class. This class provides various methods to format the date and time. In this guide, we will see the several examples of DateFormat class and later I will list down the methods and fields of this class. There is another class SimpleDateFormat that is used for the same purpose of formatting date and time.

1. DateFormat Example: Formatting the Date and Time using SHORT, MEDIUM and LONG Fields

import java.text.DateFormat;  
import java.util.Date; 
class Example{
   public static void main(String args[]){
	   
       Date currentDate = new Date();  
       System.out.println("Current date is: "+currentDate); 
       
       String dateShort = DateFormat.getDateInstance(DateFormat.SHORT).format(currentDate);  
       System.out.println("Formatting the Date using DateFormat.SHORT: "+dateShort);  
 
       String dateMedium = DateFormat.getDateInstance(DateFormat.MEDIUM).format(currentDate);  
       System.out.println("Formatting the Date using DateFormat.MEDIUM: "+dateMedium);  
         
       String dateLong = DateFormat.getDateInstance(DateFormat.LONG).format(currentDate);  
       System.out.println("Formatting the Date using DateFormat.LONG: "+dateLong); 
       
       String timeShort = DateFormat.getTimeInstance(DateFormat.SHORT).format(currentDate);  
       System.out.println("Formatting the Time using DateFormat.SHORT: "+timeShort);  
 
       String timeMedium = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(currentDate);  
       System.out.println("Formatting the Time using DateFormat.MEDIUM: "+timeMedium);  
         
       String timeLong = DateFormat.getTimeInstance(DateFormat.LONG).format(currentDate);  
       System.out.println("Formatting the Time using DateFormat.LONG: "+timeLong); 
   }
}

Output:

Current date is: Thu Oct 19 20:54:44 IST 2017
Formatting the Date using DateFormat.SHORT: 19/10/17
Formatting the Date using DateFormat.MEDIUM: 19 Oct, 2017
Formatting the Date using DateFormat.LONG: 19 October, 2017
Formatting the Time using DateFormat.SHORT: 8:54 PM
Formatting the Time using DateFormat.MEDIUM: 8:54:44 PM
Formatting the Time using DateFormat.LONG: 8:54:44 PM IST

2. DateFormat Example: Converting the Date to String

In this example, we are using the format() method of DateFormat class to convert the date to a String. You can refer this guide: Java – Date to String for more examples on Date to String conversion.

import java.text.DateFormat;  
import java.util.Date;  
public class Example {  
    public static void main(String[] args) {  
        Date date = new Date();  
        System.out.println("Date is: "+date);  
        String dateString = DateFormat.getDateInstance().format(date);  
        System.out.println("Converting Date to String: "+dateString);  
    }  
}

Output:

Date is: Thu Oct 19 21:04:16 IST 2017
Converting Date to String: 19 Oct, 2017

3. DateFormat Example: String to Date conversion using parse() method

In this example, we have given a date in String format and we are converting that String into a Date instance using the parse() method of DateFormat class. You can also refer this guide: Java – String to Date conversion for more examples.

import java.text.DateFormat;  
import java.util.Date;  
public class Example {  
    public static void main(String[] args)throws Exception {  
    	//Date in String format
    	String dateString = "11 Oct, 2017";
    	//Converting the String to date using DateFormat
        Date date = DateFormat.getDateInstance().parse(dateString); 
        //Displaying the Date
        System.out.println("Date: "+date);  
    }  
}

Output:

Date: Wed Oct 11 00:00:00 IST 2017

Methods of DateFormat class

We have seen the examples of few methods of DateFormat class. Lets see the complete list of methods available in this class.

1. final String format(Date date): We have already seen the example of this method above. This method converts the Date instance to a String.

2. Date parse(String source) throws ParseException: This method converts the String to a Date instance as we have seen in the above example.

3. static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle): Returns the date and time after formatting them based on the specified date and time styles. If you are wondering what are the styles, the SHORT, MEDIUM and LONG fields we have seen in the above examples are the styles that we can use on both date and time.

4. static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale): Same as above, here we can also specify the Locale along with date and time styles.

5. static final DateFormat getInstance(): This method returns the formatted date and time using the SHORT style for both date and time.

6. static final DateFormat getDateInstance(): Returns the formatted date for the default Locale.

7. static final DateFormat getDateInstance(int style): Same as above but here we can specify the style for the date.

8. static final DateFormat getDateInstance(int style, Locale locale): Returns the formatted date for the specified style and Locale.

9. static final DateFormat getDateTimeInstance(): Returns date and time after formatting them using default style and default Locale.

10. static final DateFormat getTimeInstance(): Returns the formatted time for the default Locale.

11. static final DateFormat getTimeInstance(int style): Same as above but here we can specify the style for the time.

12. static final DateFormat getTimeInstance(int style, Locale locale): Returns the formatted time for the specified style and Locale.

13. static Locale[] getAvailableLocales(): This method returns the array of available locales.

14. Calendar getCalendar(): Returns the Calendar instance for the current instance of DateFormat.

15. NumberFormat getNumberFormat(): Returns the NumberFormat instance for the current instance of DateFormat.

16. TimeZone getTimeZone(): Returns the timeZone for the current DateFormat instance.

Reference

DateFormat – JavaDoc

Related Posts:

  1. Java Date Format Validation
  2. Java – Parse Date to the desired Format
  3. Java – Date Formatting with TimeZone
  4. Java – Add Days to a Date
  5. Java – Convert String to 24 hour date and time format

Top Related Articles:

  1. Compare two dates with each other in Java
  2. Date validation in java
  3. Java Date and Time
  4. How to Parse Date in Desired format – Java Date
  5. Java Access Modifiers – Public, Private, Protected & Default

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