Java时间日期处理

  • 获取指定时间集合
/**
     * @Description //TODO 获取时间集合
     * @Date 9:22 2020/6/6
     * @param start 开始位置
     * @param end   结束位置
     * @return java.util.List<java.lang.String>
     **/
public static List<String> getDateList(int start, int end){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = getSomeDay(new Date(), start);//开始时间当天
        Date endDate = getSomeDay(new Date(), end);//结束时间
        List<String> result = new ArrayList<>();

        result.add(dateFormat.format(startDate));//当前时间
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(startDate);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(endDate);
        // 测试此日期是否在指定日期之后
        while (endDate.after(calBegin.getTime())) {
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            result.add(dateFormat.format(calBegin.getTime()));
        }
        return result;
    }

 /**
     * @Description //TODO 获取指定时间
     * @Date 10:10 2020/6/8
     * @param now 开始时间
     * @param start -1 代表前一天 0代表当天 1 代表后一天
     * @return java.util.Date
     **/
public static Date getSomeDay(Date now, int start){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(Calendar.DATE, start);
        return calendar.getTime();
    }


  • Date转LocalDateTime
Date nowdate = new Date();
Instant instant = nowdate.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
System.out.println(localDateTime);
  • LocalDateTime转Date
//LocalDateTime 转Date格式
LocalDateTime now2 = LocalDateTime.now();
Date from = Date.from(now2.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime 转Date格式  " + from);
  • Date操作
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

// Date 转 字符串
String dataStr = simpleDateFormat.format(new Date());
System.out.println(dataStr);

// 字符串 转 Date
Date date = simpleDateFormat.parse("2020-05-15 09:10:00");
System.out.println(date);

Date now = new Date();
// Date 加 1 天
Date addDays = DateUtils.addDays(now, 1);

// Date 加 33 分钟
Date addMinutes = DateUtils.addMinutes(now, 33);

// Date 减去 233 秒
Date addSeconds = DateUtils.addSeconds(now, -233);

//时间1
Date date1 = new Date();
//时间2
Date date2 = new Date();
// 判断两个时间是否是同一时间
boolean sameDay = DateUtils.isSameDay(date1, date2);

// 过滤时分秒,若 now 为 2020-05-10 22:13:00 调用 truncate 方法以后
// 返回时间为 2020-05-10 00:00:00
Date truncate = DateUtils.truncate(now, Calendar.DATE);
  • LocalDateTime使用
LocalDateTime nowTime = LocalDateTime.now();
//获取年
int year = nowTime.getYear();
//获取月
int moth = nowTime.getMonth().getValue();
//周几
int value = nowTime.getDayOfWeek().getValue();
//获取日
int daty = nowTime.getDayOfMonth();
//本月几号
int dayOfMonth = nowTime.getDayOfMonth();
//小时 几点
int hour = nowTime.getHour();
//分钟
int minute = nowTime.getMinute();
//秒
int second = nowTime.getSecond();
//毫秒
int nano = nowTime.getNano();

// 获取一年之前.minusYears(1) 这个 1 代表一年之前,如果是2就是两年之前
LocalDateTime minusYears = nowTime.minusYears(1);
System.out.println(minusYears);
LocalDateTime plusYears = nowTime.plusYears(1);//一年之后
System.out.println(plusYears);
LocalDateTime minusMonths = nowTime.minusMonths(1);//一个月前
System.out.println(minusMonths);
LocalDateTime plusMonths = nowTime.plusMonths(1);//一个月后
System.out.println(plusMonths);
LocalDateTime BeforeWeek = nowTime.minusWeeks(1);//一个星期前
System.out.println(BeforeWeek);
LocalDateTime AfterWeek = nowTime.plusWeeks(1);//一个星期后
System.out.println(AfterWeek);
LocalDateTime minusDays = nowTime.minusDays(1);//一天前
System.out.println(minusDays);
LocalDateTime plusDays = nowTime.plusDays(1);//一天后
System.out.println(plusDays);
LocalDateTime minusHours = nowTime.minusHours(1);//一个小时前
System.out.println(minusHours);
LocalDateTime plusHours = nowTime.plusHours(1);//一个小时后
System.out.println(plusHours);
LocalDateTime minusMinutes = nowTime.minusMinutes(1);//一分钟前
System.out.println(minusMinutes);
LocalDateTime plusMinutes = nowTime.plusMinutes(1);//一分钟后
System.out.println(plusMinutes);
LocalDateTime minusSeconds = nowTime.minusSeconds(1);//一秒前
System.out.println(minusSeconds);
LocalDateTime plusSeconds = nowTime.plusSeconds(1);//一秒后
System.out.println(plusSeconds);
LocalDateTime minusTime = nowTime.minus(23, ChronoUnit.MONTHS);//当前时间减少23个月
System.out.println(minusTime);
  • LocalDate判断
//判断时间是否相等
LocalDate now3 = LocalDate.now();
LocalDate of = LocalDate.of(2020, 5, 15);
if(now3.equals(of)){
System.out.println("111");
}
  • LocalDate-LocalTime时间增量
// 时间增量两小时
LocalTime time = LocalTime.now();
LocalTime newTime = time.plusHours(2);
System.out.println("时间增量两小时  " + newTime);


LocalDate date3 = LocalDate.now();
LocalDate localDate3 = date3.plusYears(1);
System.out.println("日期增量一年  " + localDate3);

LocalDate localDate1 = date3.plusMonths(1);
System.out.println("日期增量一个月  " + localDate1);

LocalDate localDate2 = date3.plusWeeks(1);
System.out.println("日期增量一周  " + localDate2);

LocalDate localDate = date3.plusDays(1);
System.out.println("日期增量一天时间  " + localDate);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java提供了许多用于处理日期时间的类和方法。其中最常用的是: 1. Date类:表示日期时间的类。现在已经被废弃,不建议使用。 2. Calendar类:是一个抽象类,提供了处理日期时间的方法。 3. SimpleDateFormat类:可以将日期时间格式化成指定的字符串。 4. LocalDateTime类:Java 8引入的新类,用于表示日期时间,提供了许多方便的方法。 5. Duration类和Period类:用于表示时间间隔。 下面是一些示例代码: 1. 获取当前日期时间 ```java Date date = new Date(); System.out.println(date); LocalDateTime now = LocalDateTime.now(); System.out.println(now); ``` 2. 格式化日期时间 ```java SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(date); System.out.println(formattedDate); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedNow = now.format(formatter); System.out.println(formattedNow); ``` 3. 计算时间间隔 ```java LocalDateTime start = LocalDateTime.of(2021, 1, 1, 0, 0, 0); LocalDateTime end = LocalDateTime.now(); Duration duration = Duration.between(start, end); System.out.println(duration.toDays()); LocalDate startDate = LocalDate.of(2021, 1, 1); LocalDate endDate = LocalDate.now(); Period period = Period.between(startDate, endDate); System.out.println(period.getYears()); ``` 这里只是简单介绍了一些Java处理日期时间的基础知识,具体的使用方法还需要根据实际需求进行学习和掌握。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冒险的梦想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值