LocalDateTime 时间类

来源:互联网 发布:淘宝直播怎么看回放 编辑:程序博客网 时间:2024/06/05 01:02

时间的更新切换,直接上代码,以前的方法 不好用,会出现显示不正确,当然如果你没有设置今天,昨天,前天的话,方法就可以不用这么麻烦,就是日期计算的问题



String startTime ="2016-11-13 23:59";
String endtime = "2016-11-16 00:00";
DateTimeFormatter sf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime startLocalDate = LocalDateTime.parse(startTime, sf);
LocalDateTime mid = startLocalDate.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime mid1 = startLocalDate.plusDays(2).withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime mid2 = startLocalDate.plusDays(3).withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endLocalDate = LocalDateTime.parse(endtime, sf);
long sec =  ChronoUnit.SECONDS.between(startLocalDate, endLocalDate);  
long sec1 =  ChronoUnit.SECONDS.between(startLocalDate, mid);
long sec2=  ChronoUnit.SECONDS.between(startLocalDate, mid1);
long sec3 =  ChronoUnit.SECONDS.between(startLocalDate, mid2);
if(sec<sec1){
if(sec<60){
System.out.println("刚刚");
}else{
System.out.println("今天"+startLocalDate.toString().substring(startLocalDate.toString().indexOf("T")+1));
}
}
if(sec>=sec1 && sec<sec2){
System.out.println("昨天"+startLocalDate.toString().substring(startLocalDate.toString().indexOf("T")+1));
}
if(sec>=sec2 && sec<sec3){
System.out.println("前天"+startLocalDate.toString().substring(startLocalDate.toString().indexOf("T")+1));
}
if(sec>=sec3){
System.out.println(startLocalDate.toString().replace("T", " "));
}

}

0 0
原创粉丝点击