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

    package com.rynk.commons.util;
    
    import com.rynk.commons.exception.resolver.exception.ParameterErrorException;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    
    /**
     * 时间格式化工具
     * 
     * @author ZHANGYUKUNUP
     *
     */
    public class DateUtils {
    	private static final List<String> formarts = new ArrayList<>(4);
    	static {
    		formarts.add("yyyy-MM");
    		formarts.add("yyyy-MM-dd");
    		formarts.add("yyyy-MM-dd HH:mm");
    		formarts.add("yyyy-MM-dd HH:mm:ss");
    		formarts.add("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    	}
    
    	/**
    	 * 格式化日期
    	 * 
    	 * @param dateStr String 字符型日期
    	 * @param format  String 格式
    	 * @return Date 日期
    	 */
    	private static Date parseDate(String dateStr, String format) {
    		Date date = null;
    		try {
    			DateFormat dateFormat = new SimpleDateFormat(format);
    			date = dateFormat.parse(dateStr);
    		} catch (Exception e) {
    			e.printStackTrace();
    			throw new ParameterErrorException("日期解析错误");
    		}
    		return date;
    	}
    
    	/**
    	 * 把字符串类型的时间转换成 Date对象 支持的 格式如下 
    	 * 1 yyyy-MM 
    	 * 2 yyyy-MM-dd 
    	 * 3 yyyy-MM-dd HH:mm 
    	 * 4 yyyy-MM-dd HH:mm:ss 
    	 * 5 字数字符串的时间毫秒数
    	 * 6 yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
    	 * @param dateStr
    	 * @return
    	 */
    	public static Date parse(String dateStr) {
    		if (dateStr == null) {
    			return null;
    		}
    		String value = dateStr.trim();
    		if ("".equals(value)) {
    			return null;
    		}
    
    		if (value.matches("^\d{4}-\d{1,2}$")) {
    			return parseDate(value, formarts.get(0));
    		} else if (value.matches("^\d{4}-\d{1,2}-\d{1,2}$")) {
    			return parseDate(value, formarts.get(1));
    		} else if (value.matches("^\d{4}-\d{1,2}-\d{1,2} {1}\d{1,2}:\d{1,2}$")) {
    			return parseDate(value, formarts.get(2));
    		} else if (value.matches("^\d{4}-\d{1,2}-\d{1,2} {1}\d{1,2}:\d{1,2}:\d{1,2}$")) {
    			return parseDate(value, formarts.get(3));
    		} else if (value.matches("^\d{1,19}$")) {
    			return new Date(Long.parseLong(value));
    		} else if (value.matches("^\d{4}-\d{1,2}-\d{1,2}T\d{1,2}:\d{1,2}:\d{1,2}.\d{1,3}Z$")) {
    			return parseDate(value, formarts.get(4));
    		} else {
    			throw new ParameterErrorException("不支持的时间格式");
    		}
    
    	}
    
    	/**
    	 * 格式化 时间(默认按照最常见的时间格式化输出)
    	 * 
    	 * @param date
    	 * @return
    	 */
    	public static String format(Date date) {
    		return format(date, 3);
    	}
    
    	/**
    	 * 格式化时间
    	 * 
    	 * @param date
    	 * @param i    用第几种 格式化方式
    	 * @return
    	 */
    	public static String format(Date date, int i) {
    		if (date == null) {
    			return null;
    		}
    
    		if (i > formarts.size() - 1) {
    			return null;
    		}
    
    		DateFormat dateFormat = new SimpleDateFormat(formarts.get(i));
    		return dateFormat.format(date);
    	}
    
    	/**
    	 * 取 指定日期当天第一秒的时间(00:00:00)
    	 * 
    	 * @param date
    	 * @return
    	 */
    	public static Date getBeginTime(Date date) {
    		return parse(format(date, 1) + " 00:00:00");
    	}
    
    	/**
    	 * 取指定日期当天最后最后一秒的时间(23:59:59)
    	 * 
    	 * @param date
    	 * @return
    	 */
    	public static Date getEndTime(Date date) {
    		return parse(format(date, 1) + " 23:59:59");
    	}
    	
    	/**
    	  *    取 后面 几天的 当前时间
    	 * @param date
    	 * @param dayNum 天数
    	 * @return
    	 */
    	public static Date nextDayNow(Date date, int dayNum) {
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(date);
    		
    		calendar.add(  Calendar.DAY_OF_MONTH , dayNum );
    		return calendar.getTime();
    	}
    	
    	
    	/**
    	 * 得到年月日
    	 * 下标 的0,1,2 对应年月日
    	 * @param date
    	 * @return
    	 */
    	public static int[] getYMD(Date date) {
    		String[] ymd = format(date, 1).split("-");
    		return new int[]{ Integer.valueOf( ymd[0] ),Integer.valueOf( ymd[1] ) ,Integer.valueOf( ymd[2] )    };
    	}
    
    	/**
    	 * 上月开始的时间
    	 */
    	public static Date lastMonthBeginTime() throws ParseException {
    		Calendar c = Calendar.getInstance();
    		c.add(Calendar.MONTH, -1);
    		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01 00:00:00");
    		return org.apache.commons.lang3.time.DateUtils.parseDate(sdf2.format(c.getTime()), Locale.CHINA, "yyyy-MM-dd HH:mm:ss");
    	}
    
    	/**
    	 * 本月开始的时间
    	 */
    	public static Date monthBeginTime() throws ParseException {
    		Calendar c = Calendar.getInstance();
    		c.add(Calendar.MONTH, 0);
    		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01 00:00:00");
    		return org.apache.commons.lang3.time.DateUtils.parseDate(sdf2.format(c.getTime()), Locale.CHINA, "yyyy-MM-dd HH:mm:ss");
    	}
    	
    	
    	/**
    	 * 得到指定月份的最后一天
    	 * @param year
    	 * @param month
    	 * @return
    	 */
    	public static Date getLastDayOfMonth(int year,int month){
    	        Calendar cal = Calendar.getInstance();
    	        //设置年份
    	        cal.set(Calendar.YEAR,year);
    	        //设置月份
    	        cal.set(Calendar.MONTH, month-1);
    	        //获取某月最大天数
    	        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    	        //设置日历中月份的最大天数
    	        cal.set(Calendar.DAY_OF_MONTH, lastDay);
    	          
    	        return cal.getTime();
    	}
    	
    	/**
    	 * 得到指定月份的第一天
    	 * @param year
    	 * @param month
    	 * @return
    	 */
    	public static Date getFirstDayOfMonth(int year,int month){
    		Calendar cal = Calendar.getInstance();
    		//设置年份
    		cal.set(Calendar.YEAR,year);
    		//设置月份
    		cal.set(Calendar.MONTH, month-1);
    		//获取某月最大天数
    		//设置日历中月份的最大天数
    		cal.set(Calendar.DAY_OF_MONTH, 1);
    		
    		return cal.getTime();
    	}
    	
    	
    }
    

      

  • 相关阅读:
    【服务器】【Windows】【3】开放服务器端口
    【服务器】【Windows】【2】把jar包做成服务,在Service中管理
    FZU 1753
    poj 1017
    poj 1666
    poj 1132
    ZOJ 2562 More Divisors
    POJ 2992 Divisors
    poj 2773 happy 2006
    poj 2407 Relatives
  • 原文地址:https://www.cnblogs.com/cxygg/p/12162210.html
Copyright © 2011-2022 走看看