| | |
| | | |
| | | public class TimeCalculatorUtils { |
| | | |
| | | public static int getTimeHour(){ |
| | | //如何获取当前时间的小时数(24小时制): |
| | | //获取当前时间的小时数(24小时制): |
| | | public static int nowTimeHour(){ |
| | | // 获取当前时间戳 |
| | | long timestamp = System.currentTimeMillis(); |
| | | |
| | |
| | | return calendar.get(java.util.Calendar.HOUR_OF_DAY); |
| | | } |
| | | |
| | | public static int getTimeHour(Date date){ |
| | | //获取当天小时数(24小时制)中国北京时间: |
| | | public static int timeTimeHour(Date date){ |
| | | // 创建TimeZone对象,设置为中国北京时间 |
| | | TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai"); |
| | | |
| | |
| | | return calendar.get(java.util.Calendar.HOUR_OF_DAY); |
| | | } |
| | | |
| | | public static Date getYesterday(Date date){ |
| | | //获取前一天的日期(日期天数减一) |
| | | public static Date timeYesterday(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | calendar.add(Calendar.DAY_OF_MONTH, -1); |
| | | return calendar.getTime(); |
| | | } |
| | | |
| | | public static String getYestMonthDay(Date date){ |
| | | //获取年月日 |
| | | public static String timeYestMonthDay(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | |
| | | return year+"年"+month+"月"+day+"日"; |
| | | } |
| | | |
| | | public static int getDifferenceDayInt(Date startDay,Date endDay){ |
| | | return getDifferenceDayLong(startDay,endDay).intValue(); |
| | | //获取年 |
| | | public static int timeYest(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | | return calendar.get(Calendar.YEAR); |
| | | } |
| | | |
| | | public static Long getDifferenceDayLong(Date startDay,Date endDay){ |
| | | LocalDate localDateA = dateToLocalDate(startDay); |
| | | LocalDate localDateB = dateToLocalDate(endDay); |
| | | //获取月 |
| | | public static int timeMonth(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | | return getDifferenceDays(localDateA, localDateB); |
| | | return calendar.get(Calendar.MONTH) + 1; |
| | | } |
| | | |
| | | public static Long getDifferenceDayDouble(String startDay,String endDay){ |
| | | // 定义日期字符串的格式 |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日ahh:mm"); |
| | | //获取日 |
| | | public static int timeDay(Date date){ |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.setTime(date); |
| | | |
| | | return calendar.get(Calendar.DAY_OF_MONTH); |
| | | } |
| | | //获取年差异 |
| | | public static int DifferenceYest(Date startDay,Date endDay){ |
| | | int startYest = timeYest(startDay); |
| | | int endYest = timeYest(endDay); |
| | | return endYest-startYest; |
| | | } |
| | | //获取月差异 |
| | | public static int DifferenceMonth(Date startDay,Date endDay){ |
| | | int startMonth = timeMonth(startDay); |
| | | int endMonth = timeMonth(endDay); |
| | | return endMonth-startMonth; |
| | | } |
| | | //获取天差异 |
| | | public static int DifferenceDayInt(Date startDay,Date endDay){ |
| | | return DifferenceDayLong(startDay,endDay).intValue(); |
| | | } |
| | | //获取天差异 |
| | | public static Long DifferenceDayLong(Date startDay,Date endDay){ |
| | | |
| | | LocalDate a = LocalDate.of(timeYest(startDay), timeMonth(startDay), timeDay(startDay)); |
| | | LocalDate b = LocalDate.of(timeYest(endDay), timeMonth(endDay), timeDay(endDay)); |
| | | |
| | | return ChronoUnit.DAYS.between(a, b)+1; |
| | | } |
| | | //获取天差异 |
| | | public static Double DifferenceDayMorningAfternoon(Date startDay,Date endDay,int startTime,int endTime){ |
| | | double between = DifferenceDayLong(startDay, endDay).doubleValue(); |
| | | if (startTime==2){ |
| | | between = between-0.5; |
| | | } |
| | | if (endTime == 1){ |
| | | between = between-0.5; |
| | | } |
| | | return between; |
| | | } |
| | | |
| | | public static boolean CompareData(Date startDay,Date endDay){ |
| | | |
| | | // 将日期字符串解析为LocalDate对象 |
| | | LocalDate dateA = LocalDate.parse(startDay, formatter); |
| | | LocalDate dateB = LocalDate.parse(endDay, formatter); |
| | | LocalDate a = LocalDate.of(timeYest(startDay), timeMonth(startDay), timeDay(startDay)); |
| | | LocalDate b = LocalDate.of(timeYest(endDay), timeMonth(endDay), timeDay(endDay)); |
| | | |
| | | // 计算日期对象之间的相隔天数 |
| | | return ChronoUnit.DAYS.between(dateA, dateB); |
| | | |
| | | } |
| | | |
| | | private static String getDatePart(String dateTime) { |
| | | int index = dateTime.indexOf(" "); |
| | | if (index != -1) { |
| | | return dateTime.substring(0, index); |
| | | // 比较日期 |
| | | if (a.isAfter(b)) { |
| | | return false; |
| | | } else if (a.isBefore(b)) { |
| | | return true; |
| | | } else { |
| | | return true; |
| | | } |
| | | return dateTime; |
| | | } |
| | | |
| | | private static String getTimePart(String dateTime) { |
| | | int index = dateTime.indexOf(" "); |
| | | if (index != -1) { |
| | | return dateTime.substring(index + 1); |
| | | } |
| | | return ""; |
| | | } |
| | | |
| | | private static LocalDate dateToLocalDate(Date date) { |
| | | return date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate(); |
| | | } |
| | | |
| | | private static long getDifferenceDays(LocalDate dateA, LocalDate dateB) { |
| | | return ChronoUnit.DAYS.between(dateA, dateB); |
| | | } |
| | | } |