import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;import java.util.Date; public class TimeCompare { public static void main(String[] args) { // String time1 = "2019-06-26 19:00:00"; // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//有漏洞,建议不要用 // Date date1; // try { // date1 = sdf.parse(time1); // Date date2 = new Date(); // System.out.println(date1.before(date2)); // System.out.println(date1.after(date2)); // } catch (ParseException e) { // e.printStackTrace(); // } //第一种 String time1 = "2019-06-26 19:00:00"; DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); Localdateti to localdateti to = , if localdatetime.p (time1, dtf2); System.out.println (localDateTime.isBefore (LocalDateTime.now ())); // Your time is true before the current time System.out.println (localDateTime.isAfter (LocalDateTime.now ())); // In after the current time is to false
// second
the LocalDateTime LocalDateTime.now = now ();
the Duration DURATION = Duration.between (now, now);
System.out.println (DURATION);
Long Days duration.toDays = (); / / number of days difference between the
long hours = duration.toHours (); number of hours difference //
long minutes = duration.toMinutes (); number of minutes difference //
Long duration.toMillis of millis = (); // difference milliseconds
long nanos duration.toNanos = (); // number of nanoseconds difference
System.out.println (millis);
//第三种
long mills = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println(mills >= System.currentTimeMillis());
} }
java8 new features LocalDateTime string rotation time comparison size (5)
Guess you like
Origin www.cnblogs.com/zzlcome/p/11093292.html
Recommended
Ranking