java8时间工具类
作者:互联网
import java.text.DecimalFormat; import java.time.*; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.temporal.TemporalAdjusters; import java.time.temporal.WeekFields; import java.util.Calendar; import java.util.Date; public class DateUtil { // private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) { System.out.println(getDayFirst(new Date())); System.out.println(getDayEnd(new Date())); System.out.println(getDayFirstTodayEight(new Date())); System.out.println(getMonthFirstDay()); System.out.println(getTimesMonthLastDay()); System.out.println(week(new Date())); System.out.println(month(new Date())); LocalDate end = LocalDate.of(2018, 12, 26); LocalDate start = LocalDate.of(1994, 10, 28); System.out.println(end.toEpochDay() - start.toEpochDay()); } // 获取传入日期的初始时间 public static Date getDayFirst(Date date) { Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); LocalDateTime today_start = LocalDateTime.of(localDate, LocalTime.MIN); ZonedDateTime zdt = today_start.atZone(zoneId); return Date.from(zdt.toInstant()); // } // 获取传入日期的 8點 public static Date getDayFirstTodayEight( Date date) { Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); LocalDateTime today_start = localDate.atTime(8,0,0); ZonedDateTime zdt = today_start.atZone(zoneId); return Date.from(zdt.toInstant()); } //獲取傳入日期的結束時間 public static Date getDayEnd(Date endTime) { Instant instant = endTime.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); LocalDateTime today_start = LocalDateTime.of(localDate, LocalTime.MAX); ZonedDateTime zdt = today_start.atZone(zoneId); return Date.from(zdt.toInstant()); } /** * 计算当前时间的年份 * * @return */ public static int year(Date date) { Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); return localDate.getYear(); } //获取传入日期的月份 public static int month(Date date) { Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); return localDate.getMonth().getValue(); } // 计算目前是一年中的第几周 public static int week(Date date) { // Instant instant = date.toInstant(); ZoneId zoneId = ZoneId.systemDefault(); LocalDate localDate = instant.atZone(zoneId).toLocalDate(); WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,1); return localDate.get(weekFields.weekOfWeekBasedYear()); } /** * 获取本月第一天的日期 * * @return */ public static Date getMonthFirstDay() { LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); LocalDateTime firstday = today_start.with(TemporalAdjusters.firstDayOfMonth()); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = firstday.atZone(zoneId); return Date.from(zdt.toInstant()); } /** * 获取本月最后一天日期 * * @return */ public static Date getTimesMonthLastDay() { LocalDateTime today_start = LocalDateTime.of(LocalDate.now(), LocalTime.MAX); LocalDateTime firstday = today_start.with(TemporalAdjusters.lastDayOfMonth()); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = firstday.atZone(zoneId); return Date.from(zdt.toInstant()); } public static Date MaxDate(Date beginDate, Date endDate) { if (endDate.getTime() > beginDate.getTime()) { return endDate; } else if (endDate.getTime() < beginDate.getTime()) { return beginDate; } return beginDate; } // /** // * 计算2个日期之间的相差的小时数 // * // * @param endDate // * @param nowDate // * @return // */ // public static Double getDateFloat(Date endDate, Date nowDate) { // // long nd = 1000 * 24 * 60 * 60; // long nh = 1000 * 60 * 60; // long nm = 1000 * 60; // // long diff = 0L; // // if (endDate.getTime() > nowDate.getTime()) { // diff = endDate.getTime() - nowDate.getTime(); // } else if (nowDate.getTime() > endDate.getTime()) { // diff = nowDate.getTime() - endDate.getTime(); // } // /** // * 计算差多少天 // */ // long day = diff / nd; // // /** // * 计算差多少小时 // */ // long hour = diff % nd / nh; // // /** // * 计算差多少分钟 // */ // long min = diff % nd % nh / nm; // Double a = min / 60.0; // // // // 计算差多少秒,输出结果 // BigDecimal b = new BigDecimal(a); // Double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); // return (Double) (day * 24 + hour + f1); // } public static String LocalDateTimeHourMinus( Date startTime, Date endTime){ ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区) Instant instantstart = startTime.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。) LocalDateTime startlocalDateTime = instantstart.atZone(zoneId).toLocalDateTime(); Instant instantend = endTime.toInstant(); LocalDateTime endlocalDateTime = instantend.atZone(zoneId).toLocalDateTime(); ZoneId zone = ZoneId.systemDefault(); Instant instantStart = startlocalDateTime.atZone(zone).toInstant(); Instant instantEnd = endlocalDateTime.atZone(zone).toInstant(); java.util.Date dateStart = Date.from(instantStart); java.util.Date dateEnd = Date.from(instantEnd); Calendar instance = Calendar.getInstance(); instance.setTime(dateStart); long timeInMillis1 = instance.getTimeInMillis(); Calendar instance2 = Calendar.getInstance(); instance2.setTime(dateEnd); long timeInMillis2 = instance2.getTimeInMillis(); //此处可根据自己需要来计算结果 天/时/分/秒 double hours = (timeInMillis2 - timeInMillis1)/1000/60/60.0; DecimalFormat df = new DecimalFormat("##.#"); String dff=df.format(hours); return dff; } //计算2个时间相差的 天数 public static long betweenourMinus( Date startTime, Date endTime) { { ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区) Instant instant = startTime.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。) LocalDateTime startlocalDateTime = instant.atZone(zoneId).toLocalDateTime(); Instant instantend = endTime.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。) LocalDateTime endlocalDateTime = instant.atZone(zoneId).toLocalDateTime(); LocalDateTime now = startlocalDateTime; LocalDateTime end1 =endlocalDateTime; Duration duration = Duration.between(now, end1); long days = duration.toDays(); //相差的天数 long hours = duration.toHours();//相差的小时数 long minutes = duration.toMinutes();//相差的分钟数 long millis = duration.toMillis();//相差毫秒数 long nanos = duration.toNanos();//相差的纳秒数 System.out.println(now); System.out.println(end1); System.out.println("发送短信耗时【 " + days + "天:" + hours + " 小时:" + minutes + " 分钟:" + millis + " 毫秒:" + nanos + " 纳秒】"); return hours; } } }
标签:return,atZone,ZoneId,时间,LocalDateTime,Date,工具,zoneId,java8 来源: https://blog.csdn.net/u013401913/article/details/102737298