zoukankan      html  css  js  c++  java
  • 日期工具类

    package com.payease.chains.risk.utils;
    
    import org.apache.commons.lang.time.DateUtils;
    
    import java.text.Format;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    /**
     * @Author : zhangwen
     * @Data : 2017/9/18
     * @Description :
     */
    public class DateUtil {
    
        //日期时间格式
        public static String FORMAT_PATTERN_DATE = "yyyy-MM-dd";
        //时分秒时间格式
        public static String FORMAT_PATTERN_TIME = "yyyy-MM-dd HH:mm:ss";
    
    
        /**
         * @Author zhangwen
         * @MethodName getDaySpace
         * @Params [fromDay, toDay]
         * @Return int
         * @Date 2017/9/18 上午10:38
         * @Desp 获取两个日期的时间差值
         */
        public static int getDaySpace(Date fromDay, Date toDay) throws Exception {
            SimpleDateFormat simpleFormat = new SimpleDateFormat(FORMAT_PATTERN_TIME);
            String fromDate = simpleFormat.format(fromDay);
            String toDate = simpleFormat.format(toDay);
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            int days = (int) ((to - from) / (1000 * 60 * 60 * 24));
            return days;
        }
    
        /**
         * @Author zhangwen
         * @MethodName getNow
         * @Params [pattern]
         * @Return java.util.Date
         * @Date 2017/9/18 上午10:40
         * @Desp 获取系统当前时间
         */
        public static Date getNow(String pattern) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(sdf.format(new Date()));
        }
    
        /**
         * @Author zhangwen
         * @MethodName parseDate
         * @Params [parseStr, pattern]
         * @Return java.util.Date
         * @Date 2017/9/18 上午10:45
         * @Desp 格式化时间 返回时间类型
         */
        public static Date parseDate(String parseStr, String pattern) throws Exception {
    
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(parseStr);
        }
    
        /**
         * @Author zhangwen
         * @MethodName formatDate
         * @Params [date, pattern]
         * @Return java.lang.String
         * @Date 2017/9/18 下午4:36
         * @Desp 时间格式化 返回字符串类型
         */
        public static String formatDate(Date date, String pattern) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.format(date);
        }
    
        public static void main(String[] args) {
            try {
                Date from = parseDate("2017-06-12", FORMAT_PATTERN_DATE);
                Date now = getNow(FORMAT_PATTERN_DATE);
                System.out.println(getDaySpace(from, now));
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 得到一天的最后一秒钟
         *
         * @param d
         * @return
         * @Author lch
         */
        public static Date endOfDay(Date d) {
            return DateUtils.addSeconds(
                    DateUtils.addDays(DateUtils.truncate(d, Calendar.DATE), 1), -1);
        }
    }
  • 相关阅读:
    Spring mvc配置
    css选择器
    网页全屏背景设计
    2018年的第一篇
    总结2016,规划2017
    Jenkins上Git ssh登陆配置
    Jenkins构建项目,JAVA_HOME is not defined correctly
    运行Jmeter.bat出错:Not able to find java executor or version. Please check your installation. errorlevel=2
    Selenium获取input值的两种方法:WebElement.getAttribute("value")和WebElement.getText()
    Jmeter
  • 原文地址:https://www.cnblogs.com/liuxiaoming123/p/8023334.html
Copyright © 2011-2022 走看看