208 SimpleDateFormat

208 SimpleDateFormat

> format,格式,例如:formatfactory,格式工厂

目的:让时间表示变成易读的形式,比如,x年x月x日 时:分:秒

(查看帮助文档)

继承体系:

- java.lang.Object

- - java.text.Format

- - - java.text.DateFormat

- - - - java.text.SimpleDateFormat

  1. SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner.
  2. It allows for formatting (date → text), parsing (text → date), and normalization.

SimpleDataFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。

它允许格式化(日期)→ 文本)解析(文本)→ 日期)和正常化。

  1. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.
  2. However, you are encouraged to create a date-time formatter with either getTimeInstance, getDateInstance, or getDateTimeInstance in DateFormat.
  3. Each of these class methods can return a date/time formatter initialized with a default format pattern. 
  4. You may modify the format pattern using the applyPattern methods as desired.
  5. For more information on using these methods, see DateFormat.

SimpleDataFormat允许您首先选择任何用户定义的日期时间格式模式。

但是,我们鼓励您使用日期格式的getTimeInstance、getDateInstance或getDateTimeInstance创建日期时间格式化程序。

每个类方法都可以返回一个用默认格式模式初始化的日期/时间格式化程序。

您可以根据需要使用applyPattern方法修改格式模式。

有关使用这些方法的详细信息,请参阅DateFormat。

Date and Time Patterns

日期和时间模式

Date and time formats are specified by date and time pattern strings. Within date and time pattern strings, unquoted letters from 'A' to 'Z' and from 'a' to 'z' are interpreted as pattern letters representing the components of a date or time string. Text can be quoted using single quotes (') to avoid interpretation. "''" represents a single quote. All other characters are not interpreted; they're simply copied into the output string during formatting or matched against the input string during parsing.

日期和时间格式由日期和时间模式字符串指定。在日期和时间模式字符串中,从“A”到“Z”以及从“A”到“Z”的无引号字母被解释为表示日期或时间字符串组件的模式字母。可以使用单引号(')引用文本以避免解释。“''”表示一个单引号。所有其他字符不被解释;它们只是在格式化期间复制到输出字符串中,或者在解析期间与输入字符串匹配。

The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved):

定义了以下模式字母(保留从“A”到“Z”以及从“A”到“Z”的所有其他字符):

例如yyyy-MM-dd HH-mm-ss,大小写敏感

SimpleDateFormat格式化和解析日期

1.格式化,从Date到String

public final String format(Date date),将日期格式化成易读的日期or时间字符串

2.解析,从String到Date

public Date parse(String source),从给定的字符串的开始解析出Date数据

--------------------------------------------------------------

(module)mySimpleDateFormat

(package)it01e208

class)SimpleDateFormatDemo

--------------------------------------------------------------

报错了

String s = sdf.format(d);//报错cannot resolve method 'format' in 'SimpleDateFormat'

解决方案:按讲师书写顺序去写,适时暂停导包、用ctrl alt v快捷键,这样写完就不报错

--------------------------------------------------------------

报错了

Parsing exception,解析异常,因为格式不一:2021-9-22 16:16:16、yyyy年MM月dd日 HH:mm:ss

--------------------------------------------------------------

package it01e208;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class SimpleDateFormatDemo {

    public static void main(String[] args) throws ParseException {

//        1.格式化,从Date到String

        //create date object to use its value

        Date d = new Date();

        //create SimpleDateFormat object to use its method

        //construction method

        //write SimpleDat and import, then write the right-part and ctrl+alt+v

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

        //use format-method

        String s = sdf.format(d);//cannot resolve method 'format' in 'SimpleDateFormat'

        System.out.println(s);//output:2021/9/22 下午3:54

        System.out.println("---");

//       2.解析,从String到Date

        //give the value of time

        String s2 = "2021年9月22日 16:16:16";

        //construction method

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

        Date d2 = sdf2.parse(s2);//alt enter,add method to method signature,将方法添加到方法签名

        System.out.println(d2);//报错:Parsing exception,解析异常,因为格式不一:2021-9-22 16:16:16、yyyy年MM月dd日 HH:mm:ss

        //output:Wed Sep 22 16:16:16 CST 2021

    }

}

[insert]构造方法与对象

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

> 以前没注意这一点,后来写别的代码,讲师把上述代码说成构造方法,而不是创建对象,我觉得困惑,因为我觉得那是创建对象。。。问了别人以后得出下列结论

右边使用构造方法,总的目的是要用构造方法创建一个对象

对象是new出来的,new的时候右边就是使用了构造方法

猜你喜欢

转载自blog.csdn.net/m0_63673788/article/details/121529893