java time processing tool class

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 description ********************
 * class       :  TimeUtil
 * @author     :  ncc
 * create time : 2017-12-19 11:03:11 am
 * @version    :  1.0  
 * description : get date and time
 * @see        :                        
 * ************************************************/   
public class TimeUtil {
	
	/* ********************************************
	 * method name   : date
	 * description : Get the current time in the specified format
	 * @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 : Get the specified format corresponding to the specified time
	 * @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 : Get the eight-digit date 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 : convert time to 8-digit date 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 : Get 6-digit current time 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 : Get 9-digit current time 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 : Get the date and time yyyyMMddHH corresponding to the specified time
	 * @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 : Get the 6-bit time HHmmss corresponding to the specified time
	 * @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 : Get the 12-bit time yyyyMMddHHmm corresponding to the specified time
	 * @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 : Get the 14-bit time yyyyMMddHHmmss corresponding to the specified time
	 * @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 : Get the time after the specified minutes after the specified time
	 * @return       : String yyyyMMddHHmm
	 * @param        : @param time12 yyyyMMddHHmm
	 * @param : @param s specifies the minute, which can be negative
	 * @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 : Get the 8-digit date yyyyMMdd of the day before the specified time
	 * @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 : Get the date a few days before the current date
	 * @param date8
	 * @for me
	 * @return
	 * @author      xiatian
	 * @version     1.0
	 * @date        Oct 21, 201310:03:05 AM
	 */
	/* ********************************************
	 * method name   : calcDay
	 * description : Get the 8-digit date yyyyMMdd a few days before the specified date yyyyMMdd
	 * @return       : String
	 * @param : @param date8 specifies the date
	 * @param : @param i previous n days
	 * @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 : Get the 8-digit date yyyyMMdd of the last day of the month
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          :
	 * ********************************************/      
	public static String getMonthLastDay() {   
        Calendar lastDate = Calendar.getInstance();   
        lastDate.set(Calendar.DATE, 1);// Set to the 1st of the current month   
        lastDate.add(Calendar.MONTH, 1);//Add one month to the 1st of the next month   
        lastDate.add(Calendar.DATE, -1);// Subtract one day to become the last day of the month   
        return new SimpleDateFormat("yyyyMMdd").format(lastDate.getTime());   
    }
	
	/* ********************************************
	 * method name   : getNextMonth
	 * description : get the next month's 8-digit date yyyyMMdd
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          :
	 * ********************************************/      
	public static String getNextMonth() {   
        Calendar c = Calendar.getInstance();   
        c.add(Calendar.MONTH, 1);// add one month
        return new SimpleDateFormat("yyyyMMdd").format(c.getTime());   
    }
	
	/* ********************************************
	 * method name   : getNextMonth
	 * description : Get the 8-digit date yyyyMMdd of the month next to the specified date, if not, push it forward until it is known, such as 20180130, return 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);// add one month
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
	}
	
	/* ********************************************
	 * method name   : getPreviousMonthEnd
	 * description : Get the last day of the previous month yyyyMMdd
	 * @return       : String
	 * @param        : @return
	 * modified      : ncc ,  2017-12-19
	 * @see          :
	 * ********************************************/      
	public static String getPreviousMonthEnd() {   
        Calendar lastDate = Calendar.getInstance();   
        lastDate.add(Calendar.MONTH, -1);// minus one month   
        lastDate.set(Calendar.DATE, 1);// Set the date to the first day of the month   
        lastDate.roll(Calendar.DATE, -1);// The date is rolled back one day, which is the last day of the month   
        return new SimpleDateFormat("yyyyMMdd").format(lastDate.getTime());   
    }
	
	/* ********************************************
	 * method name   : getTimestamp
	 * description : convert time to timestamp
	 * @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 : Get the timestamp of the current time
	 * @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 : Get the day of the week for the specified date
	 * @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 : Get the specified date yyyyMMdd as the week of the year
	 * @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 : Convert to 8-bit date yyyyMMdd according to the incoming long time
	 * @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 : Convert to 6-bit time HHmmss according to the incoming long time
	 * @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 : Convert to 14-bit date time yyyyMMddHHmmss according to the incoming long time
     * @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 : Get the time of the next month at the specified time. If the current month is the end of the month, add one to the month, and set the date to the end of the next month. If it is not the end of the month, add one to the month
     * @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);// Get the current date
		int lastDay= cal.getActualMaximum(Calendar.DAY_OF_MONTH);//Get the date at the end of this month
		if (curDay==lastDay) { // If the current month is the end of the month, add one to the month, and set the date to the end of the next month
			cal.add(Calendar.MONTH,1);
			lastDay= cal.getActualMaximum(Calendar.DAY_OF_MONTH);
			cal.set(Calendar.DAY_OF_MONTH,lastDay);
		} else { //If the current month is not the end of the month, add one to the month
			cal.add(Calendar.MONTH,1);
		}
        return cal.getTimeInMillis();
    }
    
    /* ********************************************
     * method name   : getYear
     * description : get the system year
     * @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 : Convert the input 2-digit year to a 4-digit year. The conversion rules are:
	 * Assume the year to be converted is not earlier than 100 years now, i.e. if the 2-digit year to be converted is greater than the present
	 * year, then it is from the last century, otherwise, it is considered to be from this century
	 * @return : String If the input year is not 2 digits, then return null
	 * @param : @param strYear2 2-digit year
	 * @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"));
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326467142&siteId=291194637