📜  Java中的日期格式类

📅  最后修改于: 2022-05-13 01:55:01.752000             🧑  作者: Mango

Java中的日期格式类

Java.text.DateFormat是一个抽象类,用于格式化和解析任何语言环境的日期。它允许我们将日期格式化为文本并将文本解析为日期。 DateFormat 类提供了许多功能来获取、格式化、解析默认日期/时间。

DateFormat 类扩展了 Format 类,这意味着它是 Format 类的子类。由于 DateFormat 类是一个抽象类,因此它可以用于日期/时间格式化子类,它以与语言无关的方式格式化和解析日期或时间。

班级签名:

public abstract class DateFormat extends Format

构造函数

DateFormat(): DateFormat() 是 DateFormat 类的构造函数,在创建 DateFormat 类的对象实例时调用该构造函数。

DateFormat 中的字段

S.NO.Field                                                                                                  Data Type                                                      
1.AM_PM_FIELDstatic int
2.calendarprotected Calendar
3.DATE_FIELDstatic int
4.DAY_OF_WEEK_FIELDstatic int
5.DAY_OF_WEEK_IN_MONTH_FIELDstatic int
6.DAY_OF_YEAR_FIELDstatic int
7. DEFAULTstatic int
8.ERA_FIELDstatic int
9.FULLstatic int
10.HOUR_OF_DAY0_FIELDstatic int
11.HOUR_OF_DAY1_FIELDstatic int
12.HOUR0_FIELDstatic int
13.HOUR1_FIELDstatic int
14.LONGstatic int
15.MEDIUMstatic int
16.MILLISECOND_FIELDstatic int
17.MINUTE_FIELDstatic int
18.MONTH_FIELDstatic int
19.numberFormatprotected NumberFormat
20.SECOND_FIELDstatic int
21.SHORTstatic int
22.TIMEZONE_FIELDstatic int
23. WEEK_OF_MONTH_FIELDstatic int
24. WEEK_OF_YEAR_FIELDstatic int
25.YEAR_FIELDstatic int

DateFormat 类的方法

S.No.     Method                                     Description                                         Return Type                       
1.format(Date date)This method formats a Date into a date/time stringString
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 formatterCalendar
4.getDateInstance()This method returns the date formatter with the default formatting style for the default localestatic 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 localestatic 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 zoneTimeZone

示例:用于更好地理解 DateFormat 类的Java程序

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


输出
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