209 日期工具类
【知识点】【format方法】
file:///C:/Users/13833123813/Desktop/jdk-17_doc-all/docs/api/java.base/java/text/Format.html
All Implemented Interfaces: 所有实现的接口
Serializable 可序列化的, Cloneable 可克隆的
Direct Known Subclasses:直接已知子类
DateFormat, MessageFormat, NumberFormat
public abstract class Format 公共抽象类格式
extends Object 扩展对象
implements Serializable, Cloneable 实现可序列化,可克隆
-
Format is an abstract base class for formatting locale-sensitive information such as dates, messages, and numbers. Format是一个抽象基类,用于格式化与区域设置相关的信息,如日期、消息和数字。
Format defines the programming interface for formatting locale-sensitive objects into Strings (the format method) and for parsing Strings back into objects (the parseObject method). Format定义了用于将区域设置敏感对象格式化为字符串(Format方法)和将字符串解析回对象(parseObject方法)的编程接口。
-
Generally, a format's parseObject method must be able to parse any string formatted by its format method. However, there may be exceptional cases where this is not possible. For example, a format method might create two adjacent integer numbers with no separator in between, and in this case the parseObject could not tell which digits belong to which number. 通常,格式的parseObject方法必须能够解析由其format方法格式化的任何字符串。然而,在某些特殊情况下,这是不可能的。例如,格式化方法可能会创建两个相邻的整数,中间没有分隔符,在这种情况下,parseObject无法分辨哪些数字属于哪个数字。
-
Subclassing 子类别化
【案例】日期工具类
【需求】定义一个日期工具类(DateUtils),包含2个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法
【思路】
1.定义日期工具类DateUtils
2.定义一个方法dateToString,用于把日期转换为指定格式的字符串。
返回值: String
参数: Date date,String format
3.定义一个方法stringToDate,用于字符串解析为指定格式的日期
返回值: Date
参数 String s,String format
4. 定义一个测试类(DateDemo),测试日期工具类的方法
--------------------------------------------------------------
(module)mySimpleDateFormat
(package)it02e209
(class)DateDemo,DateUtils
--------------------------------------------------------------
package it02e209;
// 1.定义日期工具类DateUtils
// 2.定义一个方法dateToString,用于把日期转换为指定格式的字符串。
// 返回值: String
// 参数: Date date,String format
// 3.定义一个方法stringToDate,用于字符串解析为指定格式的日期
// 返回值: Date
// 参数 String s,String format
// 4. 定义一个测试类(DateDemo),测试日期工具类的方法
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
//工具类的写法:构造方法私有,成员方法静态(构造方法是private的、成员方法是static的)
private DateUtils(){}
//输入Date,alt+enter,选择性导包:java.util.Date
public static String dateToString(Date date,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);//create object,it's same with Scanner.
//you create Scanner-object before you use its method
String s = sdf.format(date);//give value to s by format-method
return s;
}
public static Date stringToDate(String s,String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(s);
return d;
}
}
--------------------------------------------------------------
package it02e209;
import java.text.ParseException;
import java.util.Date;
public class DateDemo {
public static void main(String[] args) throws ParseException {
//create Date-object
Date d = new Date();
//define String-s and give value to s by dateToString-method.
// parameters are date,format("text")
String s = DateUtils.dateToString(d, "yyyy-MM-dd HH:mm");
System.out.println(s);//OUTPUT:2021-09-22 21:39
System.out.println("---");
String s2 = "1999年12月 01:00";
Date dd = DateUtils.stringToDate(s2,"yyyy年MM月 HH:mm");
System.out.println(dd);//OUTPUT:Wed Dec 01 01:00:00 CST 1999
String s3 = "1999年13月 01:00";
Date ddd = DateUtils.stringToDate(s3,"yyyy年MM月 HH:mm");
System.out.println(ddd);//OUTPUT:Sat Jan 01 01:00:00 CST 2000
}
}
>【insert】【导包】
输入关键词,选中关键词,按alt+enter
需要借助idea自动导包、补充代码的,不要写太快,写好前几个字母后注意下idea自动提供的选项
>【insert】自动生成创建对象公式
写好右边,按ctrl+alt+v
>【insert】【用方法,先建对象】
for example
-
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
-
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date d = sdf.parse(s);