Java时间日期

2020年10月27日22:16:35

01 Java日期处理类Date

java.util包提供了Date类来封装当前的日期和时间;

注意:是java.util.Date而不是java.sql.Date

  • 构造函数
package java.util;
public class Date implements java.io.Serializable, Cloneable, Comparable<Date> {
    private transient long fastTime;
    // 当前时间
    public Date() {
        this(System.currentTimeMillis());
    }
    // 从1970年1月1日起的毫秒数作为参数 
    public Date(long date) {
        fastTime = date;
    }
}

知识拓展:transient修饰符

transient:意为短暂的,瞬时的。

我们都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过程,只要这个类实现了Serilizable接口,这个类的所有属性和方法都会自动序列化。

然而在实际开发过程中,我们常常会遇到这样的问题,这个类的有些属性需要序列化,而其他属性不需要被序列化,打个比方,如果一个用户有一些敏感信息(如密码,银行卡号等),为了安全起见,不希望在网络操作(主要涉及到序列化操作,本地序列化缓存也适用)中被传输,这些信息对应的变量就可以加上transient关键字。换句话说,这个字段的生命周期仅存于调用者的内存中而不会写到磁盘里持久化

总之,java的transient关键字为我们提供了便利,你只需要实现Serilizable接口,将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。

transient使用小结:

  1. 一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。
  2. transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。
  3. 被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。

  • 常见方法
/**
 * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT
 * represented by this <tt>Date</tt> object.
 */
public long getTime() {
    ......
    // 即拿到时间戳
}
/**
 * @return  <code>true</code> if and only if the instant represented
 *          by this <tt>Date</tt> object is strictly later than the
 *          instant represented by <tt>when</tt>;
 *          <code>false</code> otherwise.
 */
public boolean after(Date when) {
    ......
}
/**
 * @return  <code>true</code> if and only if the instant of time
 *            represented by this <tt>Date</tt> object is strictly
 *            earlier than the instant represented by <tt>when</tt>;
 *          <code>false</code> otherwise.
 */
public boolean before(Date when) {
    ......
}

02 JDK8时间日期处理类

  • java.time.LocalDate 不含具体时间,只包含日期;

  • java.time.LocalTime 不含日期,只包含具体时间;

  • java.time.LocalDateTime 日期+时间;

    • LocalDate常用API
    • LocalTime 常用API(类似,用的时候去查就好)
    • LocalDateTime 常用API(类似,用的时候去查就好)
private LocalDate(int year, int month, int dayOfMonth) {
    this.year = year;
    this.month = (short) month;
    this.day = (short) dayOfMonth;
}
public static LocalDate now() {
    return now(Clock.systemDefaultZone());
}

构造函数被私有化了,不能用new,要用这个:LocalDate time = LocalDate.now();

public class LocalDateTest {
    public static void main(String[] args) {
        LocalDate time = LocalDate.now();
        System.out.println("现在时间: " + time);
        System.out.println("年: " + time.getYear());
        System.out.println("月(英文): " + time.getMonth());
        System.out.println("月(数字): " + time.getMonthValue());
        System.out.println("日: " + time.getDayOfMonth());
        System.out.println("周几: " + time.getDayOfWeek());
        // 加减年份,加后返回的对象才是修改后的,旧的依旧是旧的
        LocalDate changeDate = time.plusYears(1);
        System.out.println(changeDate);
        System.out.println(time);
        // 日期比较
        System.out.println(changeDate.isAfter(time));
    }
}

03 JDK8时间日期格式化

  • 常用的占位符
y  四位数年年份
M  月
d  日
h  时 在上午或下午 (1~12)
H  时 在一天中 (0~23)
m  分
s  秒
S  毫秒

JDK1.8引入的LocalDateTimeDateTimeFormatter:This class is immutable and thread-safe.

  • 时间日期格式化类:java.time.format.DateTimeFormatter
public class DateFormatterTest {
    public static void main(String[] args) {
        LocalDateTime time = LocalDateTime.now();
        // 输出格式:2020-10-27T21:53:03.647
        System.out.println(time);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatTime = dtf.format(time);
        // 输出格式:2020-10-27 21:53:03
        System.out.println("格式化后的时间: " + formatTime);
    }
}
  • 计算时间日期差类:java.time.Duration
public class TimeDurationTest {
    public static void main(String[] args) {
        LocalDateTime time = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        LocalDateTime now = LocalDateTime.now();
        // 2020-10-27T22:10:25.746
        System.out.println(now);
        // 注意:第二个参数 减 第一个参数
        Duration duration = Duration.between(now, time);
        System.out.println(duration);
        System.out.println("相差天数: " + duration.toDays());
        System.out.println("相差小时: " + duration.toHours());
        System.out.println("相差分钟: " + duration.toMinutes());
        System.out.println("相差毫秒: " + duration.toMillis());
        System.out.println("相差纳秒: " + duration.toNanos());
    }
}
posted @ 2020-10-27 22:17  ddhhdd  阅读(158)  评论(0)    收藏  举报