• DateUtil


    package com.huawei.it.gts.ls.utils;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.time.Instant;
    import java.time.LocalDate;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    import java.time.format.DateTimeFormatter;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.Locale;
    
    /**
     * 时间工具类
     * @author z00438584
     */
    public class DateUtil {
    
        public static final String DATE_TIME_FORMAT_DEFALUT = "yyyy/MM/dd HH:mm:ss";
    
        public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";
    
        public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    
        public static String formatFullTime(LocalDateTime localDateTime) {
            return formatFullTime(localDateTime, FULL_TIME_PATTERN);
        }
    
        public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
            return localDateTime.format(dateTimeFormatter);
        }
    
        private static String getDateFormat(Date date, String dateFormatType) {
            SimpleDateFormat simformat = new SimpleDateFormat(dateFormatType);
            return simformat.format(date);
        }
    
        public static String formatCSTTime(String date, String format) throws ParseException {
            SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
            Date d = sdf.parse(date);
            return DateUtil.getDateFormat(d, format);
        }
    
        public static int daysBetween(Date start, Date end) {
            Calendar calStart = Calendar.getInstance();
            Calendar calEnd = Calendar.getInstance();
            calStart.setTime(start);
            calEnd.setTime(end);
            // 设置时间为0时
            calStart.set(Calendar.HOUR_OF_DAY, 0);
            calStart.set(Calendar.MINUTE, 0);
            calStart.set(Calendar.SECOND, 0);
            calEnd.set(Calendar.HOUR_OF_DAY, 0);
            calEnd.set(Calendar.MINUTE, 0);
            calEnd.set(Calendar.SECOND, 0);
            // 得到相差的天数
            int days = ((int) (calEnd.getTime().getTime() / 1000) - (int) (calStart.getTime().getTime() / 1000)) / 3600 / 24;
            return days;
        }
    
        public static Date getDateAddDay(Date date, int days) {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, days);
            return c.getTime();
        }
    
        public static String getDateStringByFormat(Date date, String dateFormat) {
            if (date == null) {
                return null;
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
            return simpleDateFormat.format(date);
        }
    
        public static int dateCompare(Date date1, Date date2) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            String dateFirst = dateFormat.format(date1);
            String dateLast = dateFormat.format(date2);
            int dateFirstIntVal = Integer.parseInt(dateFirst);
            int dateLastIntVal = Integer.parseInt(dateLast);
            if (dateFirstIntVal > dateLastIntVal) {
                return 1;
            } else if (dateFirstIntVal < dateLastIntVal) {
                return -1;
            }
            return 0;
        }
    
        /**
         * Date转成LocalDateTime,使用系统默认时区
         */
        public static LocalDateTime date2LocalDateTime(Date date){
            // An instantaneous point on the time-line.(时间线上的一个瞬时点。)
            Instant instant = date.toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            //A time-zone ID, such as {@code Europe/Paris}.(时区)
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
            return localDateTime;
        }
    
        /**
         * LocalDateTime转成Date,使用系统默认时区
         */
        public static Date localDateTime2Date( LocalDateTime localDateTime){
            ZoneId zoneId = ZoneId.systemDefault();
            // Combines this date-time with a time-zone to create a  ZonedDateTime.
            ZonedDateTime zdt = localDateTime.atZone(zoneId);
            Date date = Date.from(zdt.toInstant());
            return date;
        }
    
        /** LocalDateTime转成String */
        public static String localDateTime2Str(LocalDateTime localDateTime, String formatStr) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(formatStr);
            String localDateTimeStr = df.format(localDateTime);
            return localDateTimeStr;
        }
    
        /** LocalDateTime转成String,默认格式:yyyy/MM/dd HH:mm:ss */
        public static String localDateTime2Str(LocalDateTime localDateTime) {
            return localDateTime2Str(localDateTime, DATE_TIME_FORMAT_DEFALUT);
        }
    
        /**
         * 日期时间字符串转LocalDateTime
         * @author xWX787141
         * @param dateTime 时间字符串
         * @param format 时间字符串格式,必须带时分秒
         * @return
         */
        public static LocalDateTime string2DateTime(String dateTime,String format) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
            return LocalDateTime.parse(dateTime,df);
        }
    
        /**
         * 日期时间字符串转LocalDate
         * @author xWX787141
         * @param date 时间字符串
         * @param format 时间字符串格式,不带时分秒
         * @return
         */
        public static LocalDate string2Date(String date,String format) {
            DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
            return LocalDate.parse(date,df);
        }
    
    }
  • 相关阅读:
    点子
    evil idea
    ubuntu 10.04.3 modify source.list
    点子
    ubuntu常用软件安装
    架构技术介绍网站
    点子
    点子
    【转发】上海地区工作,全国找网络底层技术开发大牛,旅游方面的创业项目。
    文本相似度计算余弦定理和广义Jaccard系数
  • 原文地址:https://www.cnblogs.com/lhh-boke/p/13754982.html
Copyright © 2020-2023  润新知