The java.text.DateFormat is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time.
DateFormat class extends Format class that means it is a subclass of Format class. Since DateFormat class is an abstract class, therefore, it can be used for date/time formatting subclasses, which format and parses dates or times in a language-independent manner.
Class Signature:
public abstract class DateFormat extends Format
Constructor
DateFormat(): The DateFormat() is the constructor of the DateFormat class that is called when an instance of an object of DateFormat class is created.
Note: DateFormat is an abstract class therefore cannot be instantiated
Fields in DateFormat
S.NO. | Field | Data Type |
---|
1. | AM_PM_FIELD | static int |
2. | calendar | protected Calendar |
3. | DATE_FIELD | static int |
4. | DAY_OF_WEEK_FIELD | static int |
5. | DAY_OF_WEEK_IN_MONTH_FIELD | static int |
6. | DAY_OF_YEAR_FIELD | static int |
7. | DEFAULT | static int |
8. | ERA_FIELD | static int |
9. | FULL | static int |
10. | HOUR_OF_DAY0_FIELD | static int |
11. | HOUR_OF_DAY1_FIELD | static int |
12. | HOUR0_FIELD | static int |
13. | HOUR1_FIELD | static int |
14. | LONG | static int |
15. | MEDIUM | static int |
16. | MILLISECOND_FIELD | static int |
17. | MINUTE_FIELD | static int |
18. | MONTH_FIELD | static int |
19. | numberFormat | protected NumberFormat |
20. | SECOND_FIELD | static int |
21. | SHORT | static int |
22. | TIMEZONE_FIELD | static int |
23. | WEEK_OF_MONTH_FIELD | static int |
24. | WEEK_OF_YEAR_FIELD | static int |
25. | YEAR_FIELD | static int |
Methods of DateFormat Class
S.No. | Method | Description | Return Type |
---|
1. | format(Date date) | This method formats a Date into a date/time string | String |
2. | format(Date date, StringBuffer toAppendTo, FieldPosition position) | This method formats a Date into a date/time string. | StringBuffer |
3, | getCalendar() | This method returns the calendar associated with this date/time formatter | Calendar |
4. | getDateInstance() | This method returns the date formatter with the default formatting style for the default locale | static DateFormat |
5. | getDateInstance(int style) | This method returns the date formatter with the given formatting style for the given locale. | static DateFormat |
6. | getDateTimeInstance() | This method returns the date/time formatter with the default formatting style for the default locale. | static DateFormat |
7. | getDateTimeInstance(int dateStyle, int timeStyle) | This method returns the date/time formatter with the given date and time formatting styles for the default locale | static DateFormat |
8. | getInstance() | This method returns the default date/time formatter that uses the SHORT style for both the date and the time. | static DateFormat |
9. | getTimeInstance() | This method returns the time formatter with the default formatting style for the default locale. | static DateFormat |
10. | getTimeInstance(int style) | This method returns the time formatter with the given formatting style for the default locale. | static DateFormat |
11. | getTimeInstance(int style, Locale aLocale) | This method returns the time formatter with the given formatting style for the given locale. | static DateFormat |
12. | getNumberFormat() | This method returns the number formatted, which this date/time formatter uses to format and parse a time. | NumberFormat |
13. | parse(String source) | This method parses text from the beginning of the given string to produce a date. | Date |
14. | parse(String source, ParsePosition pos) | This method parses a date/time string according to the given parse position. | Date |
15. | parseObject(String source, ParsePosition pos) | This method parses text from a string to produce a Date. | Object |
16. | setCalendar(Calendar calendar) | This method sets the calendar to be used by this date format. | void |
17. | setLenient(boolean lenient) | This method specifies whether or not date/time parsing is to be lenient. | void |
18. | setTimeZone(TimeZone zone) | This method sets the time zone for the calendar of this DateFormat object. | void |
19. | getTimeZone() | This method returns the time zone | TimeZone |
Example: Java Program for better understanding of DateFormat class
Java
// import java.text package for using DateFormat class
import java.text.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// create instance of Date class
Date date = new Date();
// to format a date for our timezone
System.out.println(
"Local Date and Time is : "
+ DateFormat.getInstance().format(date));
// to format a date for different locale
System.out.println(
"Date of Canada region : "
+ DateFormat
.getDateInstance(DateFormat.LONG,
Locale.CANADA)
.format(date));
System.out.println(
"Time of Canada region : "
+ DateFormat
.getTimeInstance(DateFormat.LONG,
Locale.CANADA)
.format(date));
System.out.println(
"Date of Italy region : "
+ DateFormat
.getDateInstance(DateFormat.LONG,
Locale.ITALY)
.format(date));
System.out.println(
"Time of Italy region : "
+ DateFormat
.getTimeInstance(DateFormat.LONG,
Locale.ITALY)
.format(date));
// to get the time zone
System.out.println(
"Time zone is : "
+ DateFormat.getInstance().getTimeZone());
System.out.println(
"Local Date and Time using getDateInstance() : "
+ DateFormat.getDateInstance().format(date));
System.out.println(
"Local Date and Time using getTimeInstance() : "
+ DateFormat.getTimeInstance().format(date));
System.out.println(
"Local Date and Time using getDateTimeInstance() : "
+ DateFormat.getDateTimeInstance().format(
date));
System.out.println(
"Local Date and Time using getDateInstance(DateFormat.LONG) : "
+ DateFormat.getTimeInstance(DateFormat.LONG)
.format(date));
System.out.println(
"Local Date and Time using getDateInstance(DateFormat.MEDIUM) : "
+ DateFormat.getTimeInstance(DateFormat.MEDIUM)
.format(date));
System.out.println(
"Local Date and Time using getDateInstance(DateFormat.SHORT) : "
+ DateFormat.getTimeInstance(DateFormat.SHORT)
.format(date));
}
}
OutputLocal Date and Time is : 11/28/21, 7:17 PM
Date of Canada region : November 28, 2021
Time of Canada region : 7:17:40 p.m. UTC
Date of Italy region : 28 novembre 2021
Time of Italy region : 19:17:40 UTC
Time zone is : sun.util.calendar.ZoneInfo[id="Etc/UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
Local Date and Time using getDateInstance() : Nov 28, 2021
Local Date and Time using getTimeInstance() : 7:17:40 PM
Local Date and Time using getDateTimeInstance() : Nov 28, 2021, 7:17:40 PM
Local Date and Time using getDateInstance(DateFormat.LONG) : 7:17:40 PM UTC
Local Date and Time using getDateInstance(DateFormat.MEDIUM) : 7:17:40 PM
Local Date and Time using getDateInstance(DateFormat.SHORT) : 7:17 PM
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read