**
* Date 日期时间类:
* 类的解释
* 类 Date 表示特定的瞬间,精确到毫秒。
* 类的位置
* java.util.Date
* 类的构造器
* public Date()分配 Date 对象并初始化此对象,以表示分配它的时间(精确到毫秒)。
* public Date(long date)分配 Date 对象并初始化此对象,
* 以表示自从标准基准时间(称为“历元(epoch)”,
* 即 1970 年 1 月 1 日 00:00:00 GMT)以来的指定毫秒数。
* 类的方法
* public long getTime()
* 返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
*
*
**
* Date类的格式化和解析:
* SimpleDateFormat类:
* 类的解释:
* SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类。
* 格式化(日期 -> 文本)
* 解析(文本 -> 日期)
* 类的位置:
* java.text.SimpleDateFormat
* 类的构造器:
* public SimpleDateFormat(String pattern)
* 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
* 类的方法:
* public final String format(Date date)将一个 Date 格式化为日期/时间字符串。
* public Date parse(String source)
* throws ParseException从给定字符串的开始解析文本,以生成一个日期。该方法不使用给定字符串的整个文本。
*
* 格式化(日期 -> 文本)步骤:
* 1.定义并初始化一个Date对象
* 2.定义并初始化SimpleDateFormat格式化解析类的对象
* 3.调用格式化解析类的方法format(),得到最终格式化的文本
* 解析(文本 -> 日期)
* 1.定义并初始化SimpleDateFormat格式化解析类的对象
* 2.定义待解析的日期文本
* 3.调用格式化解析类的方法parse(),得到最终Date对象
*
public class DateDemo02 {
public static void main(String[] args) throws ParseException {
test02();
}
private static void test02() throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
String str = "2020年09月11日 15时35分59秒798毫秒";
Date parse = sdf.parse(str);
System.out.println("parse = " + parse);
}
private static void test01() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒");
String format = sdf.format(date);
System.out.println("format = " + format);
}
}
**
* Calendar类:
* 类的解释:
* 第二代日期时间类
* 类的位置
* java.util.Calendar
* 构造器:
* 因为是抽象类,不可以实例对象
* Calendar rightNow = Calendar.getInstance();
* 方法:
* public int get(int field)
* 返回给定日历字段的值。
* public void add(int field,int amount)
*
public class DateDemo03 {
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
System.out.println(c);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "年" + (month+1) +"月" + day +"日");
c.add(Calendar.YEAR,-1010);
year = c.get(Calendar.YEAR);
System.out.println(year + "年" + (month+1) +"月" + day +"日");
}
}
**
* 此类用于演示第三代日期类(1.8)
* LocalDate、LocalTime、LocalDateTime——》Calendar类
* Instant——》Date类
* DateTimeFormatter——》SimpleDateFormat类
*
*
public class DateDemo04 {
public static void main(String[] args) {
test03();
}
private static void test03() {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String str = "2020年09月11日";
LocalDate parse = LocalDate.parse(str, dtf);
System.out.println(parse);
}
private static void test02() {
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
String format = dtf.format(ldt);
System.out.println("format = " + format);
}
private static void test01() {
LocalDateTime ldt = LocalDateTime.now();
int year = ldt.getYear();
int month = ldt.getMonthValue();
int day = ldt.getDayOfMonth();
System.out.println(year + "年" + month + "月" + day + "日") ;
}
}