📜  Java日期格式

📅  最后修改于: 2020-10-01 06:46:32             🧑  作者: Mango

Java日期格式

在Java中,有两种用于格式化日期的类:DateFormat和SimpleDateFormat。

java.text.DateFormat类提供各种方法来以语言无关的方式在java中格式化和解析日期和时间。 DateFormat类是一个抽象类。 java.text.Format是父类,而java.text.SimpleDateFormat是java.text.DateFormat类的子类。

在Java中,将日期转换为字符串称为格式化,反之亦然。换句话说,格式装置日期为字符串和解析方式的字符串到日期。

java.text.DateFormat字段

protected Calendar calendar
protected NumberFormat numberFormat
public static final int ERA_FIELD
public static final int YEAR_FIELD
public static final int MONTH_FIELD
public static final int DATE_FIELD
public static final int HOUR_OF_DAY1_FIELD
public static final int HOUR_OF_DAY0_FIELD
public static final int MINUTE_FIELD
public static final int SECOND_FIELD
public static final int MILLISECOND_FIELD
public static final int DAY_OF_WEEK_FIELD
public static final int DAY_OF_YEAR_FIELD
public static final int DAY_OF_WEEK_IN_MONTH_FIELD
public static final int WEEK_OF_YEAR_FIELD
public static final int WEEK_OF_MONTH_FIELD
public static final int AM_PM_FIELD
public static final int HOUR1_FIELD
public static final int HOUR0_FIELD
public static final int TIMEZONE_FIELD
public static final int FULL
public static final int LONG
public static final int MEDIUM
public static final int SHORT
public static final int DEFAULT

java.text.DateFormat方法

No. Public Method Description
1) final String format(Date date) converts given Date object into string.
2) Date parse(String source)throws ParseException converts string into Date object.
3) static final DateFormat getTimeInstance() returns time formatter with default formatting style for the default locale.
4) static final DateFormat getTimeInstance(int style) returns time formatter with the given formatting style for the default locale.
5) static final DateFormat getTimeInstance(int style, Locale locale) returns time formatter with the given formatting style for the given locale.
6) static final DateFormat getDateInstance() returns date formatter with default formatting style for the default locale.
7) static final DateFormat getDateInstance(int style) returns date formatter with the given formatting style for the default locale.
8) static final DateFormat getDateInstance(int style, Locale locale) returns date formatter with the given formatting style for the given locale.
9) static final DateFormat getDateTimeInstance() returns date/time formatter with default formatting style for the default locale.
10) static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle) returns date/time formatter with the given date formatting style and time formatting style for the default locale.
11) static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale) returns date/time formatter with the given date formatting style and time formatting style for the given locale.
12) static final DateFormat getInstance() returns date/time formatter with short formatting style for date and time.
13) static Locale[] getAvailableLocales() returns an array of available locales.
14) Calendar getCalendar() returns an instance of Calendar for this DateFormat instance.
15) NumberFormat getNumberFormat() returns an instance of NumberFormat for this DateFormat instance.
16) TimeZone getTimeZone() returns an instance of TimeZone for this DateFormat instance.

Java DateFormat示例:日期转换为字符串

让我们看一下使用java.text.DateFormat类在Java中格式化日期和时间的简单示例。

import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current Date: "+currentDate);
String dateToStr = DateFormat.getInstance().format(currentDate);
System.out.println("Date Format using getInstance(): "+dateToStr);
}
}

输出:

Current Date: Tue Mar 31 14:37:23 IST 2015
Date Format using getInstance(): 31/3/15 2:37 PM

让我们看看使用java.text.DateFormat类在Java中格式化日期和时间的完整示例。

import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample2 {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current Date: "+currentDate);

String dateToStr = DateFormat.getInstance().format(currentDate);
System.out.println("Date Format using getInstance(): "+dateToStr);

dateToStr = DateFormat.getDateInstance().format(currentDate);
System.out.println("Date Format using getDateInstance(): "+dateToStr);

dateToStr = DateFormat.getTimeInstance().format(currentDate);
System.out.println("Date Format using getTimeInstance(): "+dateToStr);

dateToStr = DateFormat.getDateTimeInstance().format(currentDate);
System.out.println("Date Format using getDateTimeInstance(): "+dateToStr);

dateToStr = DateFormat.getTimeInstance(DateFormat.SHORT).format(currentDate);
System.out.println("Date Format using getTimeInstance(DateFormat.SHORT): "+dateToStr);

dateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(currentDate);
System.out.println("Date Format using getTimeInstance(DateFormat.MEDIUM): "+dateToStr);

dateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(currentDate);
System.out.println("Date Format using getTimeInstance(DateFormat.LONG): "+dateToStr);

dateToStr = DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT).format(currentDate);
System.out.println("Date Format using getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT): "+dateToStr);

}
}

输出:

Current Date: Tue Mar 31 14:37:23 IST 2015
Date Format using getInstance(): 31/3/15 2:37 PM
Date Format using getDateInstance(): 31 Mar, 2015
Date Format using getTimeInstance(): 2:37:23 PM
Date Format using getDateTimeInstance(): 31 Mar, 2015 2:37:23 PM
Date Format using getTimeInstance(DateFormat.SHORT): 2:37 PM
Date Format using getTimeInstance(DateFormat.MEDIUM): 2:37:23 PM
Date Format using getTimeInstance(DateFormat.LONG): 2:37:23 PM IST
Date Format using getDateTimeInstance(DateFormat.LONG,DateFormat.SHORT): 31 March, 2015 2:37 PM

Java DateFormat示例:字符串到日期

让我们看一个简单的示例,使用java.text.DateFormat类将字符串转换为日期。

import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample3 {
public static void main(String[] args)throws Exception {
Date d = DateFormat.getDateInstance().parse("31 Mar, 2015");
System.out.println("Date is: "+d);
}
}

输出:

Date is: Tue Mar 31 00:00:00 IST 2015