日期类:
- Date java.util.Date
- 构造器:
- Date() 当前时间的日期对象
- 时间的初始基准时间: 1970 年 1 月 1 日 00:00:00
- 日期对象与字符串之间转换
import java.util.Date;
public class DateDemo01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date); //2017-01-22
long time = date.getTime(); //从基准时间开始到现在
System.out.println(time);
Date date2 = new Date(time-10000);
System.out.println(date2);
/**
* boolean after(Date when)
测试此日期是否在指定日期之后。
boolean before(Date when)
测试此日期是否在指定日期之前。
*/
System.out.println(date.after(date2));
System.out.println(date.before(date2));
System.out.println(date.toString());
}
}
日期间隔类
import java.time.*;
import java.time.temporal.ChronoUnit;
public class Java8DateInterval05 {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate birthDate = LocalDate.of(1999, 3, 15);
//时期间隔 年月日
Period p = Period.between(birthDate, today);
System.out.println(p.getYears()+"年"+p.getMonths()+"月"+p.getDays()+"日");
LocalDate startDate = LocalDate.of(1993, 8, 19);
LocalDate endDate = LocalDate.of(1994, Month.JANUARY,16);
//期量单位 间隔
long between =ChronoUnit.YEARS.between(startDate, endDate);
System.out.println("两年之间的年份差 : " + between); //0 不是1不满一年不计算在内
between =ChronoUnit.MONTHS.between(startDate, endDate);
System.out.println("两月之间的差 : " + between); //4 不是5不满一月不计算在内
Instant inst1 = Instant.now();
Instant inst2 = inst1.minus(Duration.ofSeconds(10));
System.out.println("毫秒相隔 : " + Duration.between(inst1, inst2).toMillis());
System.out.println("秒相隔 : " + Duration.between(inst2,inst1).getSeconds());
}
}
java8新增的日期类
都在java.time包下
import java.time.*;
public class Java8Date03 {
public static void main(String[] args) {
//今年
System.out.println("今年"+Year.now());
//6月
System.out.println("月份:"+Month.JUNE);
//今天不包含时分秒
LocalDate today = LocalDate.now();
System.out.println("今天:"+today);
//此时此刻
LocalDateTime now = LocalDateTime.now();
System.out.println("现在:"+now);
//您的生日
LocalDate yourBirthDate = LocalDate.of(2000, Month.JUNE, 15);
System.out.println("生日:"+yourBirthDate);
//您的学习时间
LocalDateTime dateTime = LocalDateTime.of(2020, 2, 25, 12, 30,40);
System.out.println("时间:"+dateTime);
}
}
SimpleDateFormat 日期格式转换类|器
- 可以实现按需求把日期对象与字符串之间相互转换
- 2017-11-12 2017/11/12–>指向转换模板,需要指定存在默认模板
- y 年
- M 月
- d 日
- H 时24小时制 hh 12小时制
- m 分
- s 秒
- S 毫秒
- 转换方法:
- format(Date) 日期对象转换字符串
- parse(String) 字符串转为日期对象
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormat02 {
public static void main(String[] args) throws ParseException {
//默认格式的转换器
SimpleDateFormat simple = new SimpleDateFormat();
String str= simple.format(new Date());
System.out.println(str); //20-3-31 上午10:48
//执行模板格式
SimpleDateFormat simple2 = new SimpleDateFormat("yyyy/MM/dd E hh:MM:ss");
System.out.println(simple2.format(new Date()));
//2020/03/31 星期二 10:03:25 //ParseException 如果格式无法匹配抛出异常
System.out.println(simple2.parse("2020-03-31 星期二 10:03:25"));
}
}
日期格式转换器
import java.time.*;
import java.time.format.*;
public class Java8Formatter04 {
public static void main(String[] args) {
//日期转换器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
//格式化日期字符串
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
String nowStr = now.format(formatter); //放入格式器
System.out.println(nowStr);
String dateStr= "2020/02/25 11:23:04";
//转成日期
LocalDateTime date= LocalDateTime.parse(dateStr, formatter);//放入格式器
//获取日
System.out.println(date.getDayOfMonth());
}
}