zoukankan      html  css  js  c++  java
  • 时间日期格式处理

    /**
     * Copyright 2015-2025 FLY的狐狸(email:jflyfox@sina.com qq:369191470).
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *      http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     * 
     */
    
    package com.hailian.util;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    import java.util.regex.Pattern;
    
    /**
     * 日期处理
     * 
     * 
     * 2014年5月5日 下午12:00:00
     * flyfox 330627517@qq.com
     */
    public class DateUtils {
    
        public static final int SECOND = 1;
        public static final int MINUTE_SECOND = 60 * SECOND;
        public static final int HOUR_SECOND = 60 * MINUTE_SECOND;
        public static final int DAY_SECOND = 24 * HOUR_SECOND;
        public static final int WEEK_SECOND = 7 * DAY_SECOND;
    
        /** 日期格式:yyyy-MM-dd HH:mm:ss.SSS */
        public static final String YMD_HMSSS = "yyyy-MM-dd HH:mm:ss.SSS";
        /** 日期格式:yyyyMMddHHmmssSSS */
        public static final String YMDHMSSS = "yyyyMMddHHmmssSSS";
        /** 日期格式:yyyy-MM-dd HH:mm:ss */
        public static final String YMD_HMS = "yyyy-MM-dd HH:mm:ss";
        /** 日期格式:yyyy-MM-dd HH:mm */
        public static final String YMD_HM = "yyyy-MM-dd HH:mm";
        /** 日期格式:yyyyMMddHHmmss */
        public static final String YMDHMS = "yyyyMMddHHmmss";
        /** 日期格式:yyyy-MM-dd */
        public static final String YMD = "yyyy-MM-dd";
        /** 时间格式:HH:mm:ss */
        public static final String HMS = "HH:mm:ss";
    
        private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        private static SimpleDateFormat sdf_zh = new SimpleDateFormat("yyyy年MM月dd日");
        private static SimpleDateFormat sdf_en_hy = new SimpleDateFormat("dd MMMM yyyy", Locale.ENGLISH);
    
        /**
         * 默认的日期格式 .
         */
        public static final String DEFAULT_REGEX = "yyyy-MM-dd";
        /**
         * 默认的日期格式 .
         */
        public static final String DEFAULT_REGEX_YYYYMMDD = "yyyyMMdd";
        /**
         * 默认的日期格式 .
         */
        public static final String DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS = "yyyy-MM-dd HH:mm:ss";
        /**
         * 默认的DateFormat 实例
         */
        private static final EPNDateFormat DEFAULT_FORMAT = new EPNDateFormat(DEFAULT_REGEX);
        /**
         * 默认的DateFormat 实例
         */
        private static final EPNDateFormat DEFAULT_FORMAT_YYYY_MM_DD_HH_MIN_SS = new EPNDateFormat(
                DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS);
        /**
         * 默认的DateFormat 实例
         */
        private static final EPNDateFormat DEFAULT_FORMAT_YYYYMMDD = new EPNDateFormat(DEFAULT_REGEX_YYYYMMDD);
        private static Map<String, EPNDateFormat> formatMap = new HashMap<String, EPNDateFormat>();
        static {
            formatMap.put(DEFAULT_REGEX, DEFAULT_FORMAT);
            formatMap.put(DEFAULT_REGEX_YYYY_MM_DD_HH_MIN_SS, DEFAULT_FORMAT_YYYY_MM_DD_HH_MIN_SS);
            formatMap.put(DEFAULT_REGEX_YYYYMMDD, DEFAULT_FORMAT_YYYYMMDD);
        }
    
        private DateUtils() {
    
        }
    
        /**
         * 时间对象格式化成String ,等同于java.text.DateFormat.format();
         * 
         * @param date
         *            需要格式化的时间对象
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com
         * @return 转化结果
         */
        public static String format(java.util.Date date) {
            return DEFAULT_FORMAT.format(date);
        }
    
        /**
         * 时间对象格式化成String ,等同于java.text.SimpleDateFormat.format();
         * 
         * @param date
         *            需要格式化的时间对象
         * @param regex
         *            定义格式的字符串
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com    
         * @return 转化结果
         */
        public static String format(java.util.Date date, String regex) {
            return getDateFormat(regex).format(date);
        }
    
        private static EPNDateFormat getDateFormat(String regex) {
            EPNDateFormat fmt = formatMap.get(regex);
            if (fmt == null) {
                fmt = new EPNDateFormat(regex);
                formatMap.put(regex, fmt);
            }
            return fmt;
        }
    
        /**
         * 尝试解析时间字符串 ,if failed return null;
         * 
         * @author wangp
         * @since 2008.12.20
         * @param time
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com
         * @return
         */
        public static Date parseByAll(String time) {
            Date stamp = null;
            if (time == null || "".equals(time))
                return null;
            Pattern p3 = Pattern.compile("\b\d{2}[.-]\d{1,2}([.-]\d{1,2}){0,1}\b");
            if (p3.matcher(time).matches()) {
                time = (time.charAt(0) == '1' || time.charAt(0) == '0' ? "20" : "19") + time;
            }
    
            stamp = DateUtils.parse(time, "yyyy-MM-ddHH:mm:ss");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy-MM-dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy.MM.dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy-MM");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy.MM");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy-MM-dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yy-MM-dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy.MM.dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy-MM.dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy.MM-dd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyyMMdd");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy年MM月dd日");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyyMM");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy年MM月");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy");
            if (stamp == null)
                stamp = DateUtils.parse(time, "yyyy年");
            return stamp;
        }
    
        /**
         * 解析字符串成时间 ,遇到错误返回null不抛异常
         * 
         * @param source
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com
         * @return 解析结果
         */
        public static java.util.Date parse(String source) {
            try {
                return DEFAULT_FORMAT.parse(source);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 解析字符串成时间 ,遇到错误返回null不抛异常
         * 
         * @param source
         * @param 格式表达式
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com
         * @return 解析结果
         */
        public static java.util.Date parse(String source, String regex) {
            try {
                EPNDateFormat fmt = getDateFormat(regex);
                return fmt.parse(source);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 取得当前时间的Date对象 ;
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com
         * @return
         */
        public static Date getNowDate() {
            return new Date(System.currentTimeMillis());
        }
    
        /**
         * 获取当前时间字符串
         * 
         * 2014年5月5日 下午12:00:00
         * flyfox 330627517@qq.com
         * @return
         */
        public static String getNow() {
            return getNow(DEFAULT_REGEX);
        }
    
        /**
         * 获取当前时间字符串
         * 
         * 2014年7月4日 下午11:47:10
         * flyfox 330627517@qq.com
         * @param regex 格式表达式
         * @return
         */
        public static String getNow(String regex) {
            return format(getNowDate(), regex);
        }
    
        /***
         * 获取指定时间所在天的开始时间
         * 
         * @param date
         * @return
         */
        public static String getCurrenDayBeginTime(Date date) {
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.set(Calendar.HOUR_OF_DAY, 0);
            ca.set(Calendar.MINUTE, 0);
            ca.set(Calendar.SECOND, 0);
            return format(ca.getTime(), "yyyy-MM-dd HH:mm:ss");
        }
    
        /***
         * 获取指定时间所在天的结束时间
         * 
         * @param date
         * @return
         */
        public static String getCurrenDayEndTime(Date date) {
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.set(Calendar.HOUR_OF_DAY, 23);
            ca.set(Calendar.MINUTE, 59);
            ca.set(Calendar.SECOND, 59);
            return format(ca.getTime(), "yyyy-MM-dd HH:mm:ss");
        }
    
        /**
         * 获取中文日期   yyyy年MM月dd日
         * @param dateStr
         * @return
         */
        public static String getYmdHmsssZh(String dateStr){
            try {
                Date date = sdf.parse(dateStr.trim());
                return sdf_zh.format(date);
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 获取英文日期   dd MMMM yyyy
         * @param dateStr
         * @return
         */
        public static String getYmdHmsssEn(String dateStr){
            try {
                Date date = sdf.parse(dateStr.trim());
                return sdf_en_hy.format(date);
            }catch (Exception e){
                e.printStackTrace();
            }
            return null;
        }
    
    }
    
    class EPNDateFormat {
        private SimpleDateFormat instance;
    
        EPNDateFormat() {
            instance = new SimpleDateFormat(DateUtils.DEFAULT_REGEX);
            instance.setLenient(false);
        }
    
        EPNDateFormat(String regex) {
            instance = new SimpleDateFormat(regex);
            instance.setLenient(false);
        }
    
        synchronized String format(java.util.Date date) {
            if (date == null)
                return "";
            return instance.format(date);
        }
    
        synchronized java.util.Date parse(String source) throws ParseException {
            return instance.parse(source);
        }
    }
  • 相关阅读:
    Git push 出错 [The remote end hung up unexpectedly]
    [Git高级教程(二)] 远程仓库版本回退方法
    git分支与版本管理、版本回退、冲突解决记录
    上传本地代码到gitHub过程详解
    如何用git将项目代码上传到github
    Git pull 强制覆盖本地文件
    Git忽略规则.gitignore梳理
    composer本地安装文档
    服务器通过微信公众号Token验证测试的代码(Python版)
    转载自lanceyan: 一致性hash和solr千万级数据分布式搜索引擎中的应用
  • 原文地址:https://www.cnblogs.com/dsh2018/p/10442064.html
Copyright © 2011-2022 走看看