Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaDateFormat Class in Java

DateFormat Class in Java

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));
    }
}


Output

Local 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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments