Java I18n 简明教程

Java Internationalization - SimpleDateFormat Class

java.text.SimpleDateFormat 类根据给定的模式对日期进行格式化。它还用于从字符串中解析日期,字符串中包含指定格式的日期。请参见以下使用 SimpleDateFormat 类的示例。

java.text.SimpleDateFormat class formats dates as per the given pattern. It is also used to parse dates from string where string contains date in mentioned format. See the following example of using SimpleDateFormat class.

Example

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-yyyy";

      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

      Date date = new Date();

      System.out.println(date);
      System.out.println(simpleDateFormat.format(date));

      String dateText = "29-11-2017";

      date = simpleDateFormat.parse(dateText);

      System.out.println(simpleDateFormat.format(date));
   }
}

Output

它将打印以下结果。

It will print the following result.

Fri Jun 07 15:19:00 IST 2024
07-06-2024
29-11-2017