java时间处理工具类

package com.dc.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/* ******************  类说明  *********************
 * class       :  TimeUtil
 * @author     :  ncc
 * create time :  2017-12-19 上午11:03:11
 * @version    :  1.0  
 * description :  日期时间获取
 * @see        :                        
 * ************************************************/   
public class TimeUtil {
	
	/* ********************************************
	 * method name   : date 
	 * description   : 按指定格式获取当前时间
	 * @return       : String
	 * @param        : @param fmt
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String date(String fmt) {
		return new SimpleDateFormat(fmt).format(new Date());
	}

	/* ********************************************
	 * method name   : date 
	 * description   : 获取指定时间对应的指定格式获取
	 * @return       : String
	 * @param        : @param fmt 
	 * @param        : @param t
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String date(String fmt, long t) {
		return new SimpleDateFormat(fmt).format(new Date(t));
	}

	/* ********************************************
	 * method name   : date8 
	 * description   : 获取八位日期 yyyyMMdd
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String date8() {
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}

	/* ********************************************
	 * method name   : date8 
	 * description   : 将时间转换为8位日期 yyyyMMdd
	 * @return       : String
	 * @param        : @param date
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String date8(Date date) {
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}

	/* ********************************************
	 * method name   : time6 
	 * description   : 获取6位当前时间 HHmmss
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String time6() {
		return new SimpleDateFormat("HHmmss").format(new Date());
	}
	
	/* ********************************************
	 * method name   : time9 
	 * description   : 获取9位当前时间 HHmmssSSS
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String time9() {
		return new SimpleDateFormat("HHmmssSSS").format(new Date());
	}

	/* ********************************************
	 * method name   : dateByHour 
	 * description   : 获取指定时间对应的日期时间 yyyyMMddHH
	 * @return       : String
	 * @param        : @param date
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String dateByHour( ) {
		return new SimpleDateFormat("yyyyMMddHH").format(new Date());
	}
	
	/* ********************************************
	 * method name   : time6 
	 * description   : 获取指定时间对应的6位时间 HHmmss
	 * @return       : String
	 * @param        : @param date
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String time6(Date date) {
		return new SimpleDateFormat("HHmmss").format(date);
	}
	
	/* ********************************************
	 * method name   : datetime12 
	 * description   : 获取指定时间对应的12位时间 yyyyMMddHHmm
	 * @return       : String
	 * @param        : @param date
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String datetime12(Date date) {
		return new SimpleDateFormat("yyyyMMddHHmm").format(date);
	}
	
	/* ********************************************
	 * method name   : datetime14 
	 * description   : 获取指定时间对应的14位时间 yyyyMMddHHmmss
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String datetime14( ) {
		return new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
	}
	
	/* ********************************************
	 * method name   : calcMinute 
	 * description   : 获取指定时间指定分钟之后的时间
	 * @return       : String yyyyMMddHHmm
	 * @param        : @param time12 yyyyMMddHHmm
	 * @param        : @param s 指定分钟,可为负
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String calcMinute(String time12, int s) {
		Calendar cal = Calendar.getInstance();
		cal.set(Integer.parseInt(time12.substring(0, 4)), Integer
				.parseInt(time12.substring(4, 6)) - 1, Integer.parseInt(time12
				.substring(6, 8)), Integer.parseInt(time12.substring(8, 10)),
				Integer.parseInt(time12.substring(10, 12)));
		cal.add(Calendar.MINUTE, s);
		return new SimpleDateFormat("yyyyMMddHHmm").format(cal.getTime());
	}
	
	/* ********************************************
	 * method name   : getYesterday 
	 * description   : 获取指定时间前一天的8位日期 yyyyMMdd
	 * @return       : String
	 * @param        : @param today
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getYesterday(Date today){
		Calendar cal = Calendar.getInstance();
		cal.setTime(today);
		cal.add(Calendar.DAY_OF_YEAR, -1);
		return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
	}
	/**
	 * <br>description : 获取当前日期的前几天日期
	 * @param date8
	 * @param i
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 21, 201310:03:05 AM
	 */
	/* ********************************************
	 * method name   : calcDay 
	 * description   : 获取指定日期yyyyMMdd的前几天的8位日期yyyyMMdd
	 * @return       : String
	 * @param        : @param date8 指定日期
	 * @param        : @param i 前n天
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String calcDay(String date8,int i){
		Calendar cal = Calendar.getInstance();
		cal.set(Integer.parseInt(date8.substring(0,4)),Integer.parseInt(date8.substring(4,6))-1,
				Integer.parseInt(date8.substring(6)));
		cal.add(Calendar.DAY_OF_YEAR, i);
		return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
	}
	
	/* ********************************************
	 * method name   : getMonthLastDay 
	 * description   : 获取本月最后一天的8位日期 yyyyMMdd
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getMonthLastDay() {   
        Calendar lastDate = Calendar.getInstance();   
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1号   
        lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号   
        lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天   
        return new SimpleDateFormat("yyyyMMdd").format(lastDate.getTime());   
    }
	
	/* ********************************************
	 * method name   : getNextMonth 
	 * description   : 得到下一个月的8位日期yyyyMMdd
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getNextMonth() {   
        Calendar c = Calendar.getInstance();   
        c.add(Calendar.MONTH, 1);// 加一个月
        return new SimpleDateFormat("yyyyMMdd").format(c.getTime());   
    }
	
	/* ********************************************
	 * method name   : getNextMonth 
	 * description   : 得到指定日期下一个月的8位日期yyyyMMdd,若没有,则往前推,知道有为止,如20180130,返回20180228
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getNextMonth(String date8){   
		Calendar cal = Calendar.getInstance();
		cal.set(Integer.parseInt(date8.substring(0,4)),Integer.parseInt(date8.substring(4,6))-1,
				Integer.parseInt(date8.substring(6)));
		cal.add(Calendar.MONTH, 1);// 加一个月
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime()); 
	}
	
	/* ********************************************
	 * method name   : getPreviousMonthEnd 
	 * description   : 获取上月最后一天 yyyyMMdd
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getPreviousMonthEnd() {   
        Calendar lastDate = Calendar.getInstance();   
        lastDate.add(Calendar.MONTH, -1);// 减一个月   
        lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天   
        lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天   
        return new SimpleDateFormat("yyyyMMdd").format(lastDate.getTime());   
    }
	
	/* ********************************************
	 * method name   : getTimestamp 
	 * description   : 将时间转化成时间戳
	 * @return       : String
	 * @param        : @param string
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getTimestamp(String time14) {
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyyMMddHHmmss").parse(time14);			
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (date == null) {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		}else {
			return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		}
	}
	
	/* ********************************************
	 * method name   : getTimestamp 
	 * description   : 获取当前时间的时间戳
	 * @return       : Timestamp
	 * @param        : @return
	 * modified      : ncc ,  2017-12-20
	 * @see          : 
	 * ********************************************/      
	public static Timestamp getTimestamp() {
		Date date = new Date();
		return new Timestamp(date.getTime());
	}
	
	/* ********************************************
	 * method name   : getWhatDayByDate 
	 * description   : 获取指定日期为星期几
	 * @return       : String
	 * @param        : @param date
	 * @param        : @return
	 * @param        : @throws Exception
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getWhatDayByDate(String date) throws Exception {
		Calendar calendar = Calendar.getInstance();
		Date temp = new SimpleDateFormat("yyyyMMdd").parse(date);
		calendar.setTime(temp);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
//		System.out.println(date + ":" + calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
		System.out.println(date + ":" + calendar.get(Calendar.WEEK_OF_MONTH));
		System.out.println(date + ":" + calendar.get(Calendar.WEEK_OF_YEAR));
		return dayOfWeek + "";
	}

	/* ********************************************
	 * method name   : getWeekByDate 
	 * description   : 获取指定日期yyyyMMdd为当年第几周
	 * @return       : String
	 * @param        : @param date
	 * @param        : @return
	 * @param        : @throws Exception
	 * modified      : ncc ,  2017-12-19
	 * @see          : 
	 * ********************************************/      
	public static String getWeekByDate(String date8) throws Exception {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		Date temp = null;
		try {
			temp = new SimpleDateFormat("yyyyMMdd").parse(date8);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if (temp == null) {
			temp = new Date();
		}
		calendar.setTime(temp);
		StringBuffer result = new StringBuffer();
		int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
		int year = calendar.get(Calendar.YEAR);
		if (weekOfYear == 1 && calendar.get(Calendar.MONTH) == 11) {
			year = year + 1;
		}
		result.append(year);
		if (weekOfYear < 10) {
			result.append("0");
		}
		result.append(weekOfYear);
//		System.out.println(date + ",month:" + calendar.get(Calendar.MONTH) + ",week:" + result + ",day:" + (calendar.get(Calendar.DAY_OF_WEEK)-1));
		return result.toString();
	}
	
	/* ********************************************
	 * method name   : getDateString 
	 * description   : 根据传入的long型时间转换为8位日期 yyyyMMdd
	 * @return       : String
	 * @param        : @param dt
	 * @param        : @return
	 * modified      : ncc ,  2017-12-20
	 * @see          : 
	 * ********************************************/      
	public static String getDateString(long dt) {
		SimpleDateFormat fm = new SimpleDateFormat("yyyyMMdd");
		return fm.format(new Date(dt));
	}

	/* ********************************************
	 * method name   : getTimeString 
	 * description   : 根据传入的long型时间转换为6位时间 HHmmss
	 * @return       : String
	 * @param        : @param dt
	 * @param        : @return
	 * modified      : ncc ,  2017-12-20
	 * @see          : 
	 * ********************************************/      
	public static String getTimeString(long dt) {
        SimpleDateFormat fm = new SimpleDateFormat("HHmmss"); // "yyyyMMdd G 'at' hh:mm:ss a zzz"
        return fm.format(new Date(dt));
    }
	
    /* ********************************************
     * method name   : getDateTimeString 
     * description   : 根据传入的long型时间转换为14位日期时间 yyyyMMddHHmmss
     * @return       : String
     * @param        : @param dt
     * @param        : @return
     * modified      : ncc ,  2017-12-20
     * @see          : 
     * ********************************************/      
    public static String getDateTimeString(long dt) {
        SimpleDateFormat fm = new SimpleDateFormat("yyyyMMddHHmmss"); // "yyyyMMdd G 'at' hh:mm:ss a zzz"
        return fm.format(new Date(dt));
    }
    
    /* ********************************************
     * method name   : getSpNextMonth 
     * description   : 获取指定时间下个月的时间,若当前为月末则月份加一,日期置为下个月的月末,若当前非月末则月份加一
     * @return       : long
     * @param        : @param dt
     * @param        : @return
     * modified      : ncc ,  2017-12-20
     * @see          : 
     * ********************************************/      
    public static long getNextMonth(long dt) {
    	Date d = new Date(dt);
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		int curDay = cal.get(Calendar.DAY_OF_MONTH);// 获取当前日期
		int lastDay= cal.getActualMaximum(Calendar.DAY_OF_MONTH);//获取本月月末的日期
		if (curDay==lastDay) { // 若当前为月末则月份加一,日期置为下个月的月末
			cal.add(Calendar.MONTH,1);
			lastDay= cal.getActualMaximum(Calendar.DAY_OF_MONTH);
			cal.set(Calendar.DAY_OF_MONTH,lastDay);
		} else { //若当前非月末则月份加一
			cal.add(Calendar.MONTH,1);
		}
        return cal.getTimeInMillis();
    }
    
    /* ********************************************
     * method name   : getYear 
     * description   : 获取系统年份
     * @return       : String
     * @param        : @return
     * modified      : ncc ,  2017-12-20
     * @see          : 
     * ********************************************/      
	public static String getYear() {
		return new SimpleDateFormat("yyyy").format(new Date());
	}
	
	private static final String THISCENTRUY = "20";
	private static final String LASTCENTRUY = "19";
	
	/* ********************************************
	 * method name   : getYear4ByYear2 
	 * description   : 将输入的2位年转换为4位的年,转换规则是:
	 * 					假定待转换的年不早于现在100年,即,如果待转换的2位的年大于现在
	 * 					的年份,那么说明它是上个世纪的,否则,认为是本世纪的
	 * @return       : String 如果输入年份不是2位,那么返回null
	 * @param        : @param strYear2 2位年
	 * @param        : @return
	 * modified      : ncc ,  2017-12-20
	 * @see          : 
	 * ********************************************/      
	private String getYear4ByYear2(String strYear2) {
		String strYear4 = null;
		if (null != strYear2 && strYear2.matches("\\d{2}")) {
			Calendar currentCal = Calendar.getInstance();
			int Year2 =	Integer.parseInt(Integer.toString(currentCal.get(Calendar.YEAR)).substring( 2, 4));
			int destYear2 = Integer.parseInt(strYear2);
			if (destYear2 > Year2)
				strYear4 = LASTCENTRUY + strYear2;
			else
				strYear4 = THISCENTRUY + strYear2;
		}
		return strYear4;
	}
	
	public static void main(String[] args) throws ParseException{
		  System.out.println(getNextMonth("20180130"));
	}
}

猜你喜欢

转载自decao.iteye.com/blog/2405110