学习时间函数,我们先直接学习api。
java8在线api:https://www.matools.com/api/java8
先贴图:
我们使用的时间函数,大部分都是出自于java.time这个包下,从Clock到ZoneOffset,基本上都是时间类型的抽象,而我们经常使用到的就是我圈起来的两个类。LocalDate和LocalDateTime。
看api就知道,LocalDate只包含年月日,不包含时分秒,LocalDateTime可以到具体的时分秒。下面就使用LocalDateTime简单介绍下时间函数的介绍。
1、构造和获取时间
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
public class DateUtil {
public static void main(String[] args) {
//当前时间
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
LocalDateTime localDateTime = timestamp.toLocalDateTime();
System.out.println("LocalDateTime1:"+localDateTime);
//也可以使用静态方法
System.out.println("LocalDateTime2:"+LocalDateTime.now());
System.out.println("=============================");
System.out.println("LocalDate:"+localDateTime.toLocalDate());
//格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println("格式化:"+format);
System.out.println("=============================");
LocalDateTime of = LocalDateTime.of(2020, 9, 17, 15, 25, 8);
LocalDate start = LocalDate.of(2020,7,10);//获取时间
LocalDate end = LocalDate.of(2020,9,14);
LocalDate now = LocalDate.now();//获取当前时间
System.out.println(now);
System.out.println(end.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.getDefault()));//获取星期几 取默认区域
System.out.println(now.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.ENGLISH));//获取星期几
System.out.println(now.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINESE));//获取星期几
System.out.println("=============================");
System.out.println(now.getYear());//获取年份
System.out.println(now.getMonth().getDisplayName(TextStyle.FULL, Locale.getDefault()));//获取月份
System.out.println(now.getMonth().getValue());//获取月份数字
System.out.println(now.getDayOfMonth());//当月第几天
System.out.println("=============================");
long between = end.toEpochDay()-start.toEpochDay();//获取天数间隔
System.out.println("总计天数"+between);
}
}
2.时间的计算(plus方法)
时间的计算主要是使用plus方法,图中有很多方法,看方法名就知道什么意思,个人比较喜欢使用第二个方法:
任何你要传的格式基本都能找到,非常方便
public class DateUtil {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
//格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println("格式化后时间:"+format);
//时间计算
System.out.println("下一天:"+localDateTime.plus(1, ChronoUnit.DAYS).format(dateTimeFormatter));
System.out.println("下一个月的这一天:"+localDateTime.plus(1, ChronoUnit.MONTHS).format(dateTimeFormatter));
System.out.println("前两年的这一天:"+localDateTime.plus(-2, ChronoUnit.YEARS).format(dateTimeFormatter));
}
}
还有格式化,也可以使用DateTimeFormatter的静态方法来
public class DateUtil {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
//格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println("格式化后时间:"+format);
//格式化还有另外一种方法
System.out.println("BASIC_ISO_DATE格式化方法:"+localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
System.out.println("ISO_LOCAL_DATE格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE));
System.out.println("ISO_LOCAL_TIME格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME));
System.out.println("ISO_LOCAL_DATE_TIME格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
System.out.println("ISO_DATE式化方法:"+localDateTime.format(DateTimeFormatter.ISO_DATE));
System.out.println("ISO_DATE_TIME格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
System.out.println("ISO_ORDINAL_DATE格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_ORDINAL_DATE));
System.out.println("ISO_TIME格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_TIME));
System.out.println("ISO_WEEK_DATE格式化方法:"+localDateTime.format(DateTimeFormatter.ISO_WEEK_DATE));
}
}
3.设置时间字段(with方法)
public class DateUtil {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
//格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println("格式化后时间:"+format);
System.out.println("设置时间为周一:"+localDateTime.with(DayOfWeek.MONDAY).format(dateTimeFormatter));
System.out.println("当月第一天:"+localDateTime.with(TemporalAdjusters.firstDayOfMonth()).format(dateTimeFormatter));
System.out.println("当月第18天:"+localDateTime.withDayOfMonth(18));
}
}
4.计算时间差
public class DateUtil {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
//格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format = localDateTime.format(dateTimeFormatter);
System.out.println("格式化后时间:"+format);
LocalDateTime localDateTimeEnd = localDateTime.plusDays(2);
System.out.println("两天后:"+localDateTimeEnd.format(dateTimeFormatter));
System.out.println(Duration.between(localDateTime, localDateTimeEnd).toDays());
System.out.println(Duration.between(localDateTime, localDateTimeEnd).toHours());
}
}