每天记录学习的新知识:SimpleDateFormat
发布日期:2021-05-10 05:22:56 浏览次数:16 分类:精选文章

本文共 4782 字,大约阅读时间需要 15 分钟。

SimpleDateFormat ������������

������

SimpleDateFormat ��� Java ��������������������������������������������������������������������������� DateFormat ������������ pattern ��������������������������������������������� Date ��� String ���������������������������������������������������������������������������������������������������������������������������������������������

API ������

1. ������������
public SimpleDateFormat(String pattern) {
this(pattern, Locale.getDefault(Locale.Category.FORMAT));
}
  • ��������������������������������������������������� pattern���
  • ��������������������������������������������������������� Locale ��������� FMT ���

��������� ������������������������������������������������������������������������pattern ���������������ISO 8601������������������������������

  • G ������������
  • y ������������
  • M ������������
  • d ������������
  • H ������24������������������
  • m ������������
  • s ���������
  • S ������������
  • E ���������������
  • D ���������������������������
  • F ������������������������������������
  • w ������������������������������
  • a ������������������������12������������
  • k ������24������������������
  • z ������������
2. format() ������
public String format(Date date) {
// ��� Date ��������������������� pattern ���������������
}

��������� ��������� Date ������������������������������������������������

Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatted = sdf.format(now); // ������ "2024-05-20 10:00:00"
3. parse() ������
public Date parse(String source) throws ParseException {
// ������ pattern ������������������������ Date ���������
}

��������� ������������������������������������������������������ Date ��������������������������� source ��������� pattern ������������������ ParseException ��������� ���������

String dateStr = "2019-10-10 23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parsedDate = sdf.parse(dateStr); // ���������������������������
4. applyPattern(String pattern)
public void applyPattern(String pattern) {
// ������������������������������������
}

��������� ��������������������������������������������������������������� pattern ������������ DateFormat ������������������ ���������

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.applyPattern("yyyy-MM-dd HH:mm:ss");

������������

������ 1������������������
@Test
public void dateToFormat() {
Date today = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("���������������" + sdf.format(today)); // ������������������2024-05-20 10:10:00
}
������ 2���������������������
@Test
public void timeStampToFormat() {
long currentTimeMillis = System.currentTimeMillis();
Date date = new Date(currentTimeMillis);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("���������������������������" + sdf.format(date)); // ������������������2024-05-20 10:10:00
}
������ 3���������������������
@Test
public void formatTimeZonedDateTime() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
System.out.println("������������������������" + sdf.format(now)); // ������������2024-05-20 10:10:00 ������
}
������ 4���������������������
@Test
public void parseValidateFormat() {
String dateStr = "2024-05-20 10:10:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date parsedDate = sdf.parseObject(dateStr);
System.out.println("���������������" + parsedDate);
} catch (ParseException e) {
System.out.println("���������������" + e.getMessage());
}
}
������ 5���������������������������
@Test
public void dateTimeFormatExample() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:m:s");
String inputStr = "2024-05-20 13:46:12";
try {
Date parsedDate = sdf.parseObject(inputStr);
System.out.println("���������������������������" + parsedDate);
} catch (ParseException e) {
System.out.println("���������" + e.getMessage());
}
}

������������

  • ���������������������������������SimpleDateFormat ���������������������������������������������������������������������������������
  • ������������������������������������������������������������������������������ pattern ���������������������������������������
  • ������������������������������������������������������������������������ Locale ��� TimeZone ���������
  • ������������������������ parse() ��� parseObject() ��������������������������� ParseException ���������������������������������
  • ������������

    上一篇:每天记录学习的新知识:AnalogClock模拟时钟 和 DigitalClock数字时钟
    下一篇:每天记录学习的新知识:Calendar.getInstance() 获取时间

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年04月17日 14时28分51秒