文章目录
获取当前系统时间
应用:我们可以根据这个时间戳构造唯一的文件名,当然更推荐使用UUID
//获得时间戳
long time = System.currentTimeMillis();
System.out.println(time);
Date类
Java中有两个Date
类,java.sql.Date
是java.util.Date
的子类。
对于Date掌握两个构造器和两个方法即可。
@Test
public void test1(){
//构造器一:Date()
Date date = new Date();
//方法一:toString()方法 显示当前的年月日时分秒
System.out.println(date.toString());// Mon Aug 23 21:51:27 CST 2021
//构造方法二:Date(long)
Date date1 = new Date(1629726764099L);
//方法二:getTime() 获得毫秒数
System.out.println(date1.getTime());
}
SimpleDataFormat
SimpleDateFormat
对日期的格式化和解析。我们需要掌握的操作:
- 格式化:日期–>字符串
- 解析:字符串–>日期
@Test
public void test3() throws ParseException {
//一般不使用默认的格式化器!!!!
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
//格式化
String format = sdf.format(date);
System.out.println(format);
//输出:2021-08-23 22:20:00
//解析
String str="2021-08-23 22:20:00";
Date date1 = sdf.parse(str);
System.out.println(date1);
//输出:Mon Aug 23 22:20:00 CST 2021
}
Calendar
@Test
public void test4(){
//Calendar是抽象类
//实例化Calendar
Calendar calendar = Calendar.getInstance();
//常用方法
//get() 获得一些常用的属性信息
//得到一周的第几天 一年的第几天等等
calendar.get(Calendar.DAY_OF_MONTH);
//set()
//修改Calendar对象
calendar.set(Calendar.YEAR,2030);
System.out.println(calendar);
//add()
//在原有的基础上修改calendar
calendar.add(Calendar.YEAR,3);
//Calendar与Date之间的转换
//getTime() calendar->date
Date date = calendar.getTime();//应用场景:生成特定日期的Date
//setTime() date->calendar
calendar.setTime(date);//应用场景:获得Date的各种的信息
}
注意:有偏移量(这也是不推荐使用Java8之前的日期时间类的原因之一)
JDK8之后的日期时间API
为什么不用JDK8之前的日期和时间??
- Date和Calendar里面有偏移量,很不方便;
- 对于Calendar没有格式化方法;
LocalDate LocalTime LocalDateTime
@Test
public void test1(){
//实例化
//now():获取当前的日期时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
//of():指定年月日时分秒 没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 8, 24, 1, 3, 4);
System.out.println(localDateTime1);
//如果不需要时分秒就使用LocalDate
//getXXX()
int dayOfMonth = localDateTime.getDayOfMonth();//当月的第几天
//类似的有很多
//whit() 设置相关的时间
//注意:这里的返回值是被改变的 原来的并没有被改变
//这就是不可变性
LocalDateTime localDateTime2 = localDateTime.withMonth(4);
System.out.println(localDateTime);
System.out.println(localDateTime2);
//加减操作
//plus minus 同样具有不可变性
LocalDateTime localDateTime3 = localDateTime.plusWeeks(1);
}
Instant 瞬时
相当于时间戳
@Test
public void test2(){
//创建实例
Instant instant = Instant.now();
//调用toString()方法
System.out.println(instant);
//输出:2021-08-23T17:15:35.169932900Z
//发现会有时区的问题
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//2021-08-24T01:18:25.430904500+08:00
//直接now()获取的是本初子午线上的时间戳
//获取瞬时点的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);//类似于getTime()
//通过毫秒数去构造对象 这也相当于一种实例化的方式
Instant instant1 = Instant.ofEpochMilli(1629739197961L);
System.out.println(instant1);
}
DateTimeFormatter
格式化或解析时间的 类似于SimpleDateFormatter
@Test
public void test3(){
//实例化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//格式化
String str1 = formatter.format(LocalDateTime.now());
System.out.println(str1);
//解析
String str="2021-08-24 01:35:46";
LocalDateTime parse = LocalDateTime.parse(str,formatter);
System.out.println(parse);
}