• java8中的时间处理


    java8中关于时间的处理整理

    package com.xb.utils;
    
    import java.time.*;
    import java.time.format.DateTimeFormatter;
    import java.time.temporal.ChronoUnit;
    import java.util.Date;
    
    /**
     * Author Mr. Guo
     * Create 2019/2/22 - 13:59
     */
    public class dateFormatUtils {
        //获取当天的日期(2019-02-26)
        public static LocalDate getCurrentDate = LocalDate.now();
        //获取当前的时间(18:41:32)
        public static LocalTime getCurrentTime = LocalTime.now().withNano(0);
    
        //指定时间
        public static LocalTime time(int hour, int minute, int second) {
            return LocalTime.of(hour, minute, second);
        }
    
        public static LocalTime time2(String time) {
            return LocalTime.parse(time);
        }
    
        //当前时间增加x小时
        public static LocalTime nowTimePlusxHour(int incr) {
            return dateFormatUtils.getCurrentTime.plusHours(incr);
        }
    
        public static LocalTime nowTimePlusxHosr(int incr) {
            return dateFormatUtils.getCurrentTime.plus(incr, ChronoUnit.HOURS);
        }
    
        //生日检查或账单日检查
        public static boolean judgeBirthday(int year, int month, int day, int cyear, int cmonth, int cday) {
            LocalDate birthday = LocalDate.of(year, month, day);
            MonthDay birthdayMd = MonthDay.of(birthday.getMonth(), birthday.getDayOfMonth());
            MonthDay today = MonthDay.from(LocalDate.of(cyear, cmonth, cday));
            return today.equals(birthdayMd);
        }
    
        //比较当前日期是否在指定日期之后
        public static boolean isAfterOrBefore(int year, int month, int day) {
            LocalDate today = LocalDate.now();
            LocalDate specifyDate = LocalDate.of(year, month, day);
            return today.isAfter(specifyDate);
        }
    
        //获取当前的时区(Asia/Shanghai)
        public static ZoneId currentZone = ZoneId.systemDefault();
    
        //查看指定时区的当前时间
        public static LocalDateTime pointedDateTime(String zone) {
            return LocalDateTime.now(ZoneId.of(zone));
        }
    
        //获取带有时区的时间(2019-02-26T19:21:51.669+08:00[Asia/Shanghai])
        public static ZonedDateTime zonedDateTime(String zone) {
            return ZonedDateTime.now(ZoneId.of(zone));
        }
    
        //比较两个日期的时间差
        public static int periodDate(int syear, int smonth, int sday, String condition) {
            LocalDate today = LocalDate.now();
            LocalDate specifyDate = LocalDate.of(syear, smonth, sday);
            Period period = Period.between(specifyDate, today);
            if ("day".equals(condition))
                return (int) specifyDate.until(today, ChronoUnit.DAYS);
            if ("month".equals(condition))
                return period.getMonths();
            if ("year".equals(condition))
                return period.getYears();
            return -1;
        }
    
        //日期时间格式解析、格式化
        public static LocalDate dateParse(String specifyDate) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.BASIC_ISO_DATE;
            return LocalDate.parse(specifyDate, dateTimeFormatter);
        }
    
        //转为自定义格式的时间
        public static String dateTimeFormatter(String regxDate, int cyear, int month, int cday, boolean ConverCurrentDate) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(regxDate);
            if (ConverCurrentDate)
                return dateTimeFormatter.format(LocalDate.now());
            return dateTimeFormatter.format(LocalDate.of(cyear, month, cday));
        }
    
        //时间类与Date类的相互转换
        public static Instant DateConverInstant() {
            Instant instant = Instant.now();
            Date date = Date.from(instant);
            Instant instant1 = date.toInstant();
            return instant1;
        }
    
        //date转换为LocalDateTime
        public static LocalDateTime DateToLocalDateTime() {
            Date date = new Date();
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        }
    
        //LocalDateTime转换为Date
        public static Date LocalDateTimeToDate() {
            LocalDateTime localDateTime = LocalDateTime.now();
            Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
            return Date.from(instant);
        }
    
        //LocalDate转Date
        //LocalDate不包含时间,所以转换date的时候,会默认转为当天时间的,00:00:00
        public static Date LocalDateToDate() {
            LocalDate localDate = LocalDate.now();
            Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
            return Date.from(instant);
        }
    
    }
  • 相关阅读:
    在VScode下搭载Perl的调试环境
    32.最长有效括号(Longest Valid Parentheses)
    23.合并K个排序链表(Merge k Sorted Lists)
    10.正则表达式匹配(Regular Expression Matching)
    4.寻找两个有序数组的中位数(Median of Two Sorted Arrays)
    C++中不引人瞩目的细节
    关于C++项目中头文件相互包含的问题
    关于css中hover下拉框的一个bug
    DFS-BFS(深搜广搜)原理及C++代码实现
    trie(字典树)原理及C++代码实现
  • 原文地址:https://www.cnblogs.com/Gxiaobai/p/10460394.html
Copyright © 2020-2023  润新知