Java 日期处理

一、System

获取系统当前时间毫秒值:long time = System.currentTimeMillis();,可用来做时间计算。

二、Date(java.util.Date)

Date 类代表当前所在系统的日期时间信息。

构造器说明
public Date()创建一个Date对象,系统此刻日期时间
public Date(long time)把时间毫秒值转换成Date日期对象
常用方法说明
public long getTime()返回从1970年1月1日 00:00:00走到此刻的总的毫秒数
public void setTime(long time)设置日期对象的时间为当前时间毫秒值对应的时间
public static void main(String[] args) {
    //创建Date对象,当前系统日期时间对象
    Date date = new Date();
    System.out.println(date);

    //得到时间毫秒值
    long time = date.getTime();
    System.out.println(time);

    //当前时间 +10小时20分钟30秒
    long time1 = System.currentTimeMillis();
    time1 += (10 * 60 * 60 + 20 * 60 + 30) * 1000;
    date.setTime(time1);
    System.out.println(date);

    //当前时间 +1天
    long time2 = System.currentTimeMillis();
    time2 += (24 * 60 * 60) * 1000;
    Date date2 = new Date(time2);
    System.out.println(date2);
}

三、SimpleDateFormat(java.text.SimpleDateFormat)

SimpleDateFormat 用来做日期时间的格式化操作。官方文档描述

常用构造器说明
public SimpleDateFormat​(String pattern)使用指定的格式,构造SimpleDateFormat对象
常用方法说明
public final String format(Date date)将日期格式化成日期/时间字符串
public final String format(Object time)将时间毫秒值式化成日期/时间字符串
public Date parse​(String source)给定字符串解析生成日期对象
public static void main(String[] args) {
   //以yyyy年MM月dd日 HH:mm:ss 构建SimpleDateFormat对象
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

    Date date = new Date();
    //格式化日期对象
    String rs = sdf.format(date);
    System.out.println(rs);

    //格式化时间毫秒值
    long time = System.currentTimeMillis();
    String rs1 = sdf.format(time);
    System.out.println(rs1);

    //解析字符串时间为日期对象
    String datetime = "2021-12-31 12:22:22";
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try {
        Date date1 = sdf1.parse(datetime); //抛异常
        System.out.println(date1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

四、Calendar(java.util.Calendar)

  • Calendar 代表系统此刻日期对应的日历对象。
  • Calendar 是抽象类,不能直接创建对象,使用 Calendar calender = Calendar.getInstance()
  • Calendar 是可变日期对象,一旦修改后其对象本身表示的时间将产生变化。
常用方法说明
public int get(int field)取日期中的某个字段信息
public void set(int field, int value)修改日历的某个字段信息
public void add(int field, int amount)为某个字段增加/减少指定的值
public final Date getTime()获取此刻日期对象
public long getTimeInMillis()获取此刻时间毫秒值
public static void main(String[] args) {
    // 1、拿到系统此刻日历对象
    Calendar calendar = Calendar.getInstance();
    System.out.println(calendar);

    // 2、获取日历的信息:public int get(int field):取日期中的某个字段信息。
    int year = calendar.get(Calendar.YEAR);
    System.out.println(year);

    int month = calendar.get(Calendar.MONTH) + 1;
    System.out.println(month);

    int days = calendar.get(Calendar.DAY_OF_YEAR) ;
    System.out.println(days);

    // 3、public void set(int field,int value):修改日历的某个字段信息。
    // 日期对象修改后,该对象本身表示的时间将产生变化,后面的时间都在调整后的数据上操作
    // calendar.set(Calendar.HOUR , 12);
    // System.out.println(calendar);

    // 4.public void add(int field,int amount):为某个字段增加/减少指定的值
    calendar.add(Calendar.DAY_OF_YEAR , 64);
    calendar.add(Calendar.MINUTE , 59);

    // 5.public final Date getTime(): 拿到此刻日期对象。
    Date date = calendar.getTime();
    System.out.println(date);

    // 6.public long getTimeInMillis(): 拿到此刻时间毫秒值
    long time = calendar.getTimeInMillis();
    System.out.println(time);
}

五、JDK8 新增日期/时间类

1. LocalDate、LocalTime、LocalDateTime

  • LocalDate:不包含具体时间的日期。
  • LocalTime:不含日期的时间。
  • LocalDateTime:包含了日期及时间。
  • LocalDateTime 、LocalDate 、LocalTime 类的实例都是不可变的,日期时间修改相关的方法返回的是新的实例引用。
  • 所属包:java.time
  • 点击查看官方文档,相关方法不一一举例
构建对象方法说明
LocalDate localDate = LocalDate.now();静态方法,根据当前时间创建日期对象
LocalTime localTime = LocalTime.now();静态方法,根据当前时间创建时间对象
LocalDateTime localDateTime = LocalDateTime.now();静态方法,根据当前时间创建日期时间对象
LocalDate localDate = LocalDate.of(2000, 1, 1);静态方法,指定日期创建对象
LocalTime localTime = LocalTime.of(1, 1, 1);静态方法,指定时间创建对象
LocalDateTime localDateTime = LocalDateTime.of(2000, 1, 1, 1, 1, 1);静态方法,指定日期时间创建对象

LocalDate代码示例

public static void main(String[] args) {
    // 1.静态方法,根据当前日期创建对象
    LocalDate localDate = LocalDate.now();
    System.out.println(localDate);

    // 2.指定日期创建对象
    LocalDate localDate1 = LocalDate.of(1997, 10, 1);
    System.out.println(localDate1);

    // 3.LocalDate相关方法
    System.out.println(localDate.getYear()); // 年
    System.out.println(localDate.getMonth()); // 月
    System.out.println(localDate.getMonth().getValue()); // 月
    System.out.println(localDate.getMonthValue()); // 月
    System.out.println(localDate.getDayOfMonth()); // 日
    System.out.println(localDate.getDayOfYear()); // 当年的第几天
    System.out.println(localDate.getDayOfWeek()); // 星期
    System.out.println(localDate.getDayOfWeek().getValue()); // 星期
    System.out.println("----------------");

    System.out.println(localDate.plusYears(1)); // 1年后
    System.out.println(localDate.plusMonths(1)); // 1个月后
    System.out.println(localDate.plusWeeks(1)); // 1周后
    System.out.println(localDate.plusDays(1)); // 1天后
    System.out.println("----------------");

    System.out.println(localDate.minusYears(1)); // 1年前
    System.out.println(localDate.minusMonths(1)); // 1个月前
    System.out.println(localDate.minusWeeks(1)); // 1周前
    System.out.println(localDate.minusDays(1)); // 1天前
    System.out.println("----------------");
    // 不可变对象,每次修改产生新对象!
    System.out.println(localDate);

    System.out.println("----------------");
    System.out.println(localDate.equals(localDate1)); // 判断是否同一天
    System.out.println(localDate.isAfter(localDate1)); // 判断是否在localDate1之前
    System.out.println(localDate.isBefore(localDate1)); // 判断是否在localDate1之后
}

LocalTime代码示例

public static void main(String[] args) {
    // 1.根据当前时间创建对象
    LocalTime localTime = LocalTime.now();
    System.out.println(localTime);

    // 2.指定时间创建对象
    LocalTime localTime1 = LocalTime.of(1, 1, 1, 1);
    System.out.println(localTime1);

    // 3.LocalTime相关方法
    System.out.println(localTime.getHour()); // 时
    System.out.println(localTime.getMinute()); // 分
    System.out.println(localTime.getSecond()); // 秒
    System.out.println(localTime.getNano()); // 纳秒
    System.out.println("----------------");

    System.out.println(localTime.plusHours(1)); // 1小时后
    System.out.println(localTime.plusMinutes(1)); // 1分钟后
    System.out.println(localTime.plusSeconds(1)); // 1秒后
    System.out.println(localTime.plusNanos(1)); // 1纳秒后
    System.out.println("----------------");

    System.out.println(localTime.minusHours(1)); // 1小时前
    System.out.println(localTime.minusMinutes(1)); // 1分钟前
    System.out.println(localTime.minusSeconds(1)); // 1秒前
    System.out.println(localTime.minusNanos(1)); // 1纳秒前
    System.out.println("----------------");
    // 不可变对象,每次修改产生新对象!
    System.out.println(localTime);

    System.out.println("----------------");
    System.out.println(localTime.equals(localTime1)); // 判断是否同一时刻
    System.out.println(localTime.isAfter(localTime1)); // 判断是否在localTime1之前
    System.out.println(localTime.isBefore(localTime1)); // 判断是否在localTime1之后
}

LocalDateTime代码示例

public static void main(String[] args) {
    // 1.根据当前日期时间创建对象
    LocalDateTime localDateTime = LocalDateTime.now();
    System.out.println(localDateTime);

    // 2.指定日期时间创建对象
    LocalDateTime localDateTime1 = LocalDateTime.of(2000, Month.JANUARY, 1, 1, 1, 1, 1);
    System.out.println(localDateTime1);

    // 3.LocalDateTime综合了LocalDate和LocalTime里面的方法,直接使用即可

    // 4.LocalDateTime转LocalDate
    LocalDate localDate = localDateTime1.toLocalDate();
    System.out.println(localDate);

    // 5.localDateTime转LocalTime
    LocalTime localTime = localDateTime1.toLocalTime();
    System.out.println(localTime);
}

2. Instant 时间戳

  1. 所属包:java.time
  2. Instant 类由一个静态的工厂方法 now() 可以返回当前时间戳。
    Instant instant = Instant.now();
  3. 设置返回当前系统所在时区的时间戳。
    instant.atZone(ZoneId.systemDefault())
  4. Instant 和 Date 这两个类可以进行转换。
    Date date = Date.from(instant); Instant instant1 = date.toInstant();

3. DateTimeFormatter

java.time.format.DateTimeFormatter,日期与时间格式器。

方法说明
public static DateTimeFormatter ofPattern(String pattern)静态方法,创建DateTimeFormatter 对象
public static void main(String[] args) {
    // 1.获取当前系统日期时间
    LocalDateTime ldt = LocalDateTime.now();
    System.out.println(ldt);

    // 2.构建解析/格式化器
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    System.out.println(dtf.format(ldt)); // 正向格式化
    System.out.println(ldt.format(dtf)); // 逆向格式化

    // 3.解析字符串时间,转为本地日期时间对象,再进行操作
    // 不需要抛出异常,DateTimeFormatter是线程安全的
    LocalDateTime dateTime = LocalDateTime.parse("2000-01-01 11:11:11", dtf);
    System.out.println(dateTime);
}

4. Period、Duration

  • java.time.Period,计算 日期间隔 差异,只能精确到年月日,用于 LocalDate 之间的比较。
  • java.time.Duration,计算 时间间隔 差异,提供了使用基于时间的值测量时间量的方法,用于 LocalDateTime 之间的比较。也可用于 Instant 之间的比较。
public static void main(String[] args) {
    // 1.当前系统日期
    LocalDate currentDate = LocalDate.now();
    System.out.println(currentDate);

    // 2.任取一个起始日期
    LocalDate startDate = LocalDate.of(1872, 10, 13);
    System.out.println(startDate);

    // 3.计算两个日期之间的间隔
    Period period = Period.between(startDate, currentDate);
    System.out.printf("间隔时间为 : %d 年 %d 月 %d 日\n", period.getYears(), period.getMonths(), period.getDays());

    System.out.println("-------------------------");

    // 1.当前系统时间
    LocalDateTime currentDateTime = LocalDateTime.now();
    System.out.println(currentDateTime);

    // 2.任取一个起始日期
    LocalDateTime startDateTime = LocalDateTime.of(2000, 1, 1, 23, 59, 59);
    System.out.println(startDateTime);

    // 3.计算两个时间之间的间隔
    Duration duration = Duration.between(startDateTime, currentDateTime);
    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()); //两个时间差的纳秒数
}

5. ChronoUnit

  • java.time.temporal.ChronoUnit
  • ChronoUnit 类可用于在单个时间单位内测量一段时间,最全面的工具类,可以用于比较所有的时间单位
public static void main(String[] args) {
    // 1.当前系统时间
    LocalDateTime currentDateTime = LocalDateTime.now();
    System.out.println(currentDateTime);

    // 2.任取一个起始日期
    LocalDateTime startDateTime = LocalDateTime.of(2000, 1, 1, 23, 59, 59);
    System.out.println(startDateTime);

    System.out.println("相差的年数:" + ChronoUnit.YEARS.between(startDateTime, currentDateTime));
    System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(startDateTime, currentDateTime));
    System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(startDateTime, currentDateTime));
    System.out.println("相差的天数:" + ChronoUnit.DAYS.between(startDateTime, currentDateTime));
    System.out.println("相差的时数:" + ChronoUnit.HOURS.between(startDateTime, currentDateTime));
    System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(startDateTime, currentDateTime));
    System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(startDateTime, currentDateTime));
    System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(startDateTime, currentDateTime));
    System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(startDateTime, currentDateTime));
    System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(startDateTime, currentDateTime));
    System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(startDateTime, currentDateTime));
    System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(startDateTime, currentDateTime));
    System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(startDateTime, currentDateTime));
    System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(startDateTime, currentDateTime));
    System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(startDateTime, currentDateTime));
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值