Java中与日期和时间相关的API
文章目录一、System类中的currentTimeMillis()二、java.util.Date类java.sql.Date类三、SimpleDateFormat的使用四、java.util.Calendar日历类五、LocalDate、LocalTime、LocalDateTime的使用六、Instant类的使用七、DateTimeFormatter的使用八、其他API一、System类中的
文章目录
一、System类中的currentTimeMillis()
public void test1(){
long time = System.currentTimeMillis();
//返回当前时间与1970年1月1日0时0分0秒之间的时间差,以毫秒为单位
//称为时间戳
System.out.println(time);
}
输出结果:
二、java.util.Date类
两个构造器的使用:
Date():创建一个对应当前时间的Date对象
Date(long date):创建一个指定毫秒数的Date对象
两个方法的使用:
toString():显式当前的年月日时分秒
getTime():获取当前对象时毫秒数(时间戳)
public void test2(){
//构造器一:Date():创建一个对应当前时间的Date对象
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.getTime());
//构造器二:Date(long date):创建一个指定毫秒数的Date对象
Date date2 = new Date(1595473439050L);
System.out.println(date2.toString());
System.out.println(date2.getTime());
}
输出结果:
java.sql.Date类
java.sql.Date类是java.util.Date类的子类
java.sql.Date类对应着数据库中的日期类型的变量
实例化:
public void test2(){
java.sql.Date date3 = new java.sql.Date(1595473439050L);
System.out.println(date3.toString());
}
输出结果:
如何将java.util.Date的对象转换为java.sql.Date的对象
Date date4 = new Date();
java.sql.Date date5 = new java.sql.Date(date4.getTime());
三、SimpleDateFormat的使用
SimpleDateFormat对日期Date类的两个操作:
- 格式化:日期→字符串
- 解析:格式化的逆过程,字符串→日期
SimpleDateFormat的实例化
public void test3() throws ParseException {
//实例化SimpleDateFormat,使用默认的构造器
SimpleDateFormat sdf = new SimpleDateFormat();
//格式化:日期→字符串
Date date = new Date();
System.out.println(date);
String format = sdf.format(date);
System.out.println(format);
//解析:格式化的逆过程,字符串→日期
String str = "20-7-23 下午12:25";//注意时间格式必须严格一致
Date date1 = sdf.parse(str);
System.out.println(date1);
}
上述的解析需要时间格式严格的一致,但是我们在开发中不喜欢这么严格的格式,怎么办呢?——实例化SimpleDateFormat,使用带参数的构造器
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
四、java.util.Calendar日历类
- calendar是一个抽象基类,主要用于完成日期字段之间相互操作的功能
- calendar实例化的方法:①使用其静态方法:Calendar.getInstance()方法 ②创建他的子类GregorianCalendar的对象
- 常用方法:get(), set(),add(),getTime(),setTime()
public void test4(){
//1.实例化
//方式一:创建他的子类GregorianCalendar的对象
//方式二:使用其静态方法:Calendar.getInstance()方法
Calendar calendar = Calendar.getInstance();
//2.常用方法
//get()
int days = calendar.get(Calendar.DAY_OF_MONTH);//当前日子是这个月的第几天,我这里是23天
System.out.println(days);
//set()
calendar.set(Calendar.DAY_OF_MONTH,25);//把当前日子是这个月的第23天改为第25天
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//add()
calendar.add(Calendar.DAY_OF_MONTH,3);//在原有的日子基础上 再加上3天
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
//getTime():把calendar类→date
Date date = calendar.getTime();
System.out.println(date);
//setTime():把date→calendar类
Date date1 = new Date();
calendar.setTime(date1);
days = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(days);
}
输出结果为:
注意:
- 获取月份时:一月是0,二月是1,以此类推,12月是11
- 获取星期时:周日是1,周二是2,以此类推,周六是7
五、LocalDate、LocalTime、LocalDateTime的使用
public void test5(){
//实例化的第一种方式: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(2010, 11, 27, 15, 24);
System.out.println(localDateTime1);
//getXxx():获取相关的属性
System.out.println(localDate.getDayOfMonth());
System.out.println(localDate.getDayOfWeek());
System.out.println(localDate.getDayOfYear());
//withXxx():设置相关的属性
LocalDateTime localDateTime2 = localDateTime.withDayOfMonth(12);
System.out.println(localDateTime);
System.out.println(localDateTime2);
}
输出结果:
六、Instant类的使用
public void test6(){
//now():获取本初子午线处对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);
//根据时区添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);
//toEpochMilli():获取瞬时点自1970年1月1号0时0分0秒对应的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);
//ofEpochMilli():通过给定的毫秒数,获取Instant实例
Instant instant1 = Instant.ofEpochMilli(1595490880839l);
System.out.println(instant1);
}
七、DateTimeFormatter的使用
DateTimeFormatter:格式化或解析日期、时间,类似于SimpleDateFormat
方式一:预定义的标准格式。如:ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
方式二:
本地化相关的格式,如ofLocalizedDateTime()
FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
本地化相关的格式,如ofLocalizedDate()
FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate
重点:方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
public void test7(){
//方式一:预定义的标准格式。如:ISO_LOCAL_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期→字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);
System.out.println(str1);//2020-07-23T16:17:01.236
//解析:字符串→日期
TemporalAccessor parse = formatter.parse("2020-07-23T16:17:01.236");
System.out.println(parse);
//方式二:本地化相关的格式,如ofLocalizedDateTime()
//FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);
//本地化相关的格式,如ofLocalizedDate()
//FormatStyle.FULL/FormatStyle.LONG/FormatStyle.MEDIUM/FormatStyle.SHORT:适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);
//重点:方式三:自定义的格式。如:ofPattern("yyyy-MM-dd hh:mm:ss")
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);
//解析
TemporalAccessor accessor = formatter3.parse("2020-07-23 04:27:52");
System.out.println(accessor);
}
输出结果为:
八、其他API
更多推荐
所有评论(0)