package com.tian.mvc01.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; public class TimeUtils { /** * 计算时间差值(jdk自带) * @param args */ public static void main(String[] args) { TimeUtils obj = new TimeUtils(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm:ss"); try { Date date1 = simpleDateFormat.parse("10/10/2013 11:30:00"); Date date2 = simpleDateFormat.parse("10/10/2013 20:00:00"); obj.printDifference(date1, date2); } catch (ParseException e) { e.printStackTrace(); } } //1 minute = 60 seconds //1 hour = 60 x 60 = 3600 //1 day = 3600 x 24 = 86400 public void printDifference(Date startDate, Date endDate){ //milliseconds long different = endDate.getTime() - startDate.getTime(); System.out.println("startDate : " + startDate); System.out.println("endDate : "+ endDate); System.out.println("different : " + different); long secondsInMilli = 1000; long minutesInMilli = secondsInMilli * 60; long hoursInMilli = minutesInMilli * 60; long daysInMilli = hoursInMilli * 24; long elapsedDays = different / daysInMilli; different = different % daysInMilli; long elapsedHours = different / hoursInMilli; different = different % hoursInMilli; long elapsedMinutes = different / minutesInMilli; different = different % minutesInMilli; long elapsedSeconds = different / secondsInMilli; System.out.printf( "%d days, %d hours, %d minutes, %d seconds%n", elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds); } /** * 获取时间字符串之间的小时差 * @param start * @param end * @return */ public static double getHours(String start,String end){ DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); LocalDateTime startTime = LocalDateTime.parse(start,df); LocalDateTime endTime = LocalDateTime.parse(end,df); Duration duration = Duration.between(startTime,endTime); long minutes = duration.toMinutes(); long hours = duration.toHours(); long a = minutes%60; double as = 0.0; if (a>=30||a<=-30){ as= hours+0.5; }else if (a==0){ as=hours; } return as; } /** * 得到时间段内的每一天 * @param startTime * @param endTime * @return * @throws ParseException */ public static List<Date> getEveryDay(String startTime, String endTime) throws ParseException { List<Date> list = new ArrayList<>(); Date date0 = new SimpleDateFormat("yyyy-MM-dd").parse(startTime); Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(endTime); Calendar cal = Calendar.getInstance(); cal.setTime(date0); while(cal.getTime().compareTo(date1)<=0){ Date time = cal.getTime(); list.add(time); cal.add(Calendar.DAY_OF_MONTH,1); } return list; } // 01. java.util.Date --> java.time.LocalDateTime public void UDateToLocalDateTime() { java.util.Date date = new java.util.Date(); Instant instant = date.toInstant(); ZoneId zone = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); } // 02. java.util.Date --> java.time.LocalDate public void UDateToLocalDate() { java.util.Date date = new java.util.Date(); Instant instant = date.toInstant(); ZoneId zone = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); LocalDate localDate = localDateTime.toLocalDate(); } // 03. java.util.Date --> java.time.LocalTime public void UDateToLocalTime() { java.util.Date date = new java.util.Date(); Instant instant = date.toInstant(); ZoneId zone = ZoneId.systemDefault(); LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); LocalTime localTime = localDateTime.toLocalTime(); } // 04. java.time.LocalDateTime --> java.util.Date public void LocalDateTimeToUdate() { LocalDateTime localDateTime = LocalDateTime.now(); ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); java.util.Date date = Date.from(instant); } // 05. java.time.LocalDate --> java.util.Date public void LocalDateToUdate() { LocalDate localDate = LocalDate.now(); ZoneId zone = ZoneId.systemDefault(); Instant instant = localDate.atStartOfDay().atZone(zone).toInstant(); java.util.Date date = Date.from(instant); } // 06. java.time.LocalTime --> java.util.Date public void LocalTimeToUdate() { LocalTime localTime = LocalTime.now(); LocalDate localDate = LocalDate.now(); LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); ZoneId zone = ZoneId.systemDefault(); Instant instant = localDateTime.atZone(zone).toInstant(); java.util.Date date = Date.from(instant); } }