Java 国际化 - 日期格式模式
以下是日期格式模式中字符的使用。
Sr.No. | 类别与说明 |
---|---|
1 | G 显示纪元。 |
2 | y 显示年份。有效值为 yy、yyyy。 |
3 | M 显示月份。有效值为 MM、MMM 或 MMMMM。 |
4 | d 显示月份中的日期。有效值为 d、dd。 |
5 | h 显示一天中的小时 (1-12 AM/PM)。有效值为 hh。 |
6 | H 显示一天中的小时 (0-23)。有效值为 HH。 |
7 | m 显示一小时中的分钟 (0-59)。有效值 mm。 |
8 | s 显示分钟中的秒数(0-59)。有效值 ss。 |
9 | S 显示分钟中的毫秒数(0-999)。有效值 SSS。 |
10 | E 显示星期几(例如星期一、星期二等) |
11 | D 显示一年中的天数(1-366)。 |
12 | F 显示一个月中的星期几(例如 12 月的第一个星期四)。 |
13 | w 显示一年中的周数(1-53)。 |
14 | W 显示月份中的周数(0-5) |
15 | a 显示 AM / PM |
16 | k 显示一天中的小时数(1-24)。 |
17 | K 显示一天中的小时数,AM / PM(0-11)。 |
18 | z 显示时间区域。 |
示例
在此示例中,我们根据不同的模式格式化日期。
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class I18NTester { public static void main(String[] args) throws ParseException { String pattern = "dd-MM-yy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); Date date = new Date(); System.out.println(simpleDateFormat.format(date)); pattern = "MM-dd-yyyy"; simpleDateFormat = new SimpleDateFormat(pattern); System.out.println(simpleDateFormat.format(date)); pattern = "yyyy-MM-dd HH:mm:ss"; simpleDateFormat = new SimpleDateFormat(pattern); System.out.println(simpleDateFormat.format(date)); pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"; simpleDateFormat = new SimpleDateFormat(pattern); System.out.println(simpleDateFormat.format(date)); } }
输出
它将打印以下结果。
07-06-24 06-07-2024 2024-06-07 16:04:40 Friday June 2024 16:04:40.866+0530