Java DateFormat getCalendar()方法及示例

Java DateFormat getCalendar()方法及示例

Java中 DateFormat类getCalendar() 方法是用来获取与该日期/时间格式对象相关的日历。

语法

public Calendar getCalendar()

参数: 该方法不接受任何参数。

返回值: 该方法为这个DateFormat对象返回一个Calendar的实例。

下面的程序说明了getCalendar()方法在Java中的使用:

例1 :

// Java code to illustrate
// getCalendar() method
  
import java.text.*;
import java.util.*;
  
public class DateFormat_Demo {
    public static void main(String[] argv)
    {
        // Initializing the first formatter
        DateFormat DFormat
            = DateFormat.getDateTimeInstance();
  
        // String formatting
        String str = DFormat.format(new Date());
  
        // Displaying the Calendar
        System.out.println(DFormat.getCalendar());
    }
}

输出:

java.util.GregorianCalendar[time=1553691245240, areFieldsSet=true, areAllFieldsSet=true,
lenient=true, zone=sun.util.calendar.ZoneInfo[id="Etc/UTC", offset=0, dstSavings=0,
useDaylight=false, transitions=0, lastRule=null], firstDayOfWeek=1, minimalDaysInFirstWeek=1,
ERA=1, YEAR=2019, MONTH=2, WEEK_OF_YEAR=13, WEEK_OF_MONTH=5, DAY_OF_MONTH=27, DAY_OF_YEAR=86,
DAY_OF_WEEK=4, DAY_OF_WEEK_IN_MONTH=4, AM_PM=1, HOUR=0, HOUR_OF_DAY=12, MINUTE=54, SECOND=5,
MILLISECOND=240, ZONE_OFFSET=0, DST_OFFSET=0]

例2 :

// Java code to illustrate
// getCalendar() method
  
import java.text.*;
import java.util.*;
  
public class DateFormat_Demo {
    public static void main(String[] argv)
    {
        // Initializing the first formatter
        DateFormat DFormat
            = new SimpleDateFormat("MM/ dd/ yy");
  
        // String formatting
        String str = DFormat.format(new Date());
  
        // Displaying the Calendar
        System.out.println(DFormat.getCalendar());
    }
}

输出:

java.util.GregorianCalendar[time=1553691264669, areFieldsSet=true, areAllFieldsSet=true,
lenient=true, zone=sun.util.calendar.ZoneInfo[id="Etc/UTC", offset=0, dstSavings=0,
useDaylight=false, transitions=0, lastRule=null], firstDayOfWeek=1, minimalDaysInFirstWeek=1,
ERA=1, YEAR=2019, MONTH=2, WEEK_OF_YEAR=13, WEEK_OF_MONTH=5, DAY_OF_MONTH=27, DAY_OF_YEAR=86,
DAY_OF_WEEK=4, DAY_OF_WEEK_IN_MONTH=4, AM_PM=1, HOUR=0, HOUR_OF_DAY=12, MINUTE=54, SECOND=24,
MILLISECOND=669, ZONE_OFFSET=0, DST_OFFSET=0]

参考资料: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#getCalendar()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程