Java-分享几个项目里处理时间的方法1.0

以后再用到别的还会继续补充

1、获取今天零点零分零秒的毫秒数

//当前时间毫秒数
long current = System.currentTimeMillis();
//今天零点零分零秒的毫秒数
long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset();

2、获取指定时间两小时后的毫秒数

String baseTime = "20201014130700";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(simpleDateFormat.parse(baseTime););
calendar.add(Calendar.HOUR_OF_DAY, Integer.parseInt("2"));
String twoHoursAgo = simpleDateFormat.format(calendar.getTime());

3、字符串时间格式 转换为 毫秒数

String testTime = "2020-11-21 12:23:24";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long testTimeToLong = simpleDateFormat.parse(testTime).getTime();

4、获取当前时间 N天之前 的毫秒数

public long getNDaysAgo(int n){
    long current = System.currentTimeMillis();
    long nDays = n * (24*60*60*1000L);
    return current - nDays;
}

5、毫秒数转字符串格式

long current = System.currentTimeMillis();
//2022-10-04 18-40-40
String now = simpleDateFormat.format(new Date(current)); 

6、获取下个月一号零点零时零分的毫秒数

Calendar first = Calendar.getInstance();
first.add(Calendar.MONTH, 1);
first.set(Calendar.DAY_OF_MONTH, first.getActualMinimum(Calendar.DAY_OF_MONTH));
first.set(Calendar.HOUR_OF_DAY, 0);
first.set(Calendar.MINUTE, 0);
first.set(Calendar.SECOND, 0);
first.set(Calendar.MILLISECOND, 0);
long nextMonthFirst = first.getTime().getTime();

7、将Date类型的时间 转换成 指定的时间格式

Date now = new Date();
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");
String date1 = simpleDateFormat1.format(now);
String date2 = simpleDateFormat2.format(now);

8、获取去年当月一号的数据

//2022-10-04
LocalDate today = LocalDate.now(); 
//2021-10-01
LocalDate localDate = today.minusMonths(12).with(TemporalAdjusters.firstDayOfMonth());

9、计算两个毫秒数时间相差天数

long beginTime = 1649808000000L;
long endTime = 1653494399000L;
long timeDifference = endTime- beginTime;
int days = (int)(timeDifference/(24*60*60*1000));

10、获取LocalDateTime格式下, 当天的零点

LocalDateTime now = LocalDateTime.now();
LocalDateTime zeroToday = LocalDateTime.of(LocalDate.from(now), LocalTime.MIN);

11、获取指定时间格式下,LocalDateTime格式的零点和最大时间

String b = "1997-10-22";
DateTimeFormatter dff = new DateTimeFormatterBuilder()
        .appendPattern("yyyy-MM-dd" + "[['T'HH][:mm][:ss]]")
        .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
        .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
        .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
        .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
        .toFormatter();
//零点:1997-10-22T00:00
LocalDateTime zero = LocalDateTime.parse(b, dff);
//最大时间1:1997-10-22T23:59:59.999999999
LocalDateTime bigger1 = LocalDateTime.of(LocalDate.parse(b), LocalTime.MAX);
//最大时间2:1997-10-22T23:59:59
LocalDateTime bigger2 = bi.withHour(23).withSecond(59).withMinute(59);

12、获取当前年、当前月、当前日

//当前年
int nowYear = LocalDateTime.now().getYear();
//当前月
int nowMonth = LocalDateTime.now().getMonthValue();
//当前日
int today = LocalDateTime.now().getDayOfMonth();

13、获取Instant时间格式的年、月、日

Instant nowTime = Instant.now();
DateTimeFormatter dateFormatYear = DateTimeFormatter.ofPattern("yyyy").withZone(ZoneId.systemDefault());
DateTimeFormatter dateFormatMonth = DateTimeFormatter.ofPattern("MM").withZone(ZoneId.systemDefault());
DateTimeFormatter dateFormatDay = DateTimeFormatter.ofPattern("dd").withZone(ZoneId.systemDefault());
String year = dateFormatYear.format(nowTime);
String month = dateFormatMonth.format(nowTime);
String day = dateFormatDay.format(nowTime);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值