时间工具类

时间工具类


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.baison.bap.utils.LongUtil;

/**日期工具类
 * 
 *
 */
public class DateUtil {
	/**
	 * get value of lastMonth MM
	 * 
	 * @return 上个月份
	 */
	public static synchronized String getlastMonth() {

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1); // 得到前一个月
		SimpleDateFormat simpleFormate = new SimpleDateFormat("MM");
		String lastmonth = simpleFormate.format(calendar.getTime());
		return lastmonth;
	}

	/**
	 * get last month's date
	 * 
	 * @return 上个月的时间
	 */
	public static synchronized Date getlastMonthDate(Date date) {

		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(date);// 设置当前日期
		calendar.add(Calendar.MONTH, -1);// 月份减一
		return calendar.getTime();
	}

	/**
	 * get value of year YYYY For lash month
	 * 
	 * @return 上个月所在年份
	 */
	public static synchronized int getDateWeekYear(Date date) {
		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(date);// 设置当前日期
		calendar.add(Calendar.MONTH, -1);// 月份减一
		return calendar.getWeekYear();
	}

	/**
	 * 通过int型的年份和月份获取上月的年份和月份
	 */
	public static synchronized Map getLastTime(int confirmYear, int confirmMonth) {

		Map lastTimeMap = new HashMap();
		confirmMonth -= 1;
		if (confirmMonth == 0) {
			confirmYear -=1;
			confirmMonth=12;
		}
		lastTimeMap.put("confirmYear", confirmYear);
		lastTimeMap.put("confirmMonth", confirmMonth);
		return lastTimeMap;
	}

	
	/**
	 * 获取T-n日的开始时间和结束时间
	 * @param n n为正整数
	 * */
	public static synchronized Map getYesterdayOfTDay(int n){
		Calendar calendar=Calendar.getInstance();
		calendar.add(Calendar.DATE,-n);
		Date time=calendar.getTime();
		SimpleDateFormat yesterdayOfTBegin = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		SimpleDateFormat yesterdayOfTEnd = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		Map yesterdayOfT=new HashMap<>(); //T-1日
		yesterdayOfT.put("yesterdayOfTBegin", yesterdayOfTBegin.format(time));
		yesterdayOfT.put("yesterdayOfTEnd", yesterdayOfTEnd.format(time));

		
		return yesterdayOfT;
	}
	
	
	/**
	 * 转换日期,如果参数是String类型,按"yyyy-MM-dd HH:mm:ss"转换
	 * 
	 * @see DateUtil::format
	 * @param val
	 * @return
	 * @throws ParseException
	 */
	public static synchronized Date convertToDate(Object val) {
		return convertToDate(val, DEF_DATETIME_FMT);
	}
	
	private static synchronized Date innerConvertToDate(Object val, String format) throws ParseException {
		if (val instanceof java.sql.Date) {
			return new Date(((java.sql.Date) val).getTime());
		} else if (val instanceof Date) {
			return (Date) val;
		}

		if (val instanceof Integer || val instanceof Long) {
			return new Date(LongUtil.parse(val));
		} else if (val != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(val.toString());
		}

		return null;
	}
	
	/**
	 * 转换日期
	 * 
	 * 
	 * 
	 * @param val
	 * @param format
	 *            如果参数是String类型,按此参数转换 
  • yyyy:年份
  • MM:月份
  • dd:日期
  • HH:小时 *
  • mm:分钟
  • ss:秒
  • ms:毫秒 * @return */ public static synchronized Date convertToDate(Object val, String format) { try { return innerConvertToDate(val, format); } catch (ParseException e) { return null; } } /** * * 获取-t1Month月的起始时间 * */ public static synchronized String getLastMonthBegin(int t1Month){ Calendar c=Calendar.getInstance(); c.add(Calendar.MONTH, -t1Month); // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59); //按格式输出 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01 00:00:00"); String gtime2 = sdf2.format(c.getTime()); //上月第一天 return gtime2; } /** * * 获取-t1Month月的结束时间 * */ public static synchronized String getLastMonthEnd(int t1Month){ Calendar c=Calendar.getInstance(); c.add(Calendar.MONTH, -t1Month); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); int lastMonthMaxDay=c.getActualMaximum(Calendar.DAY_OF_MONTH); c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59); //按格式输出 String gtime = sdf.format(c.getTime()); //上月最后一天 return gtime; } public static void main(String[] args) { // Map lastTimeMap = getLastTime(2018, 12); // System.out.println(lastTimeMap.get("confirmYear")); // System.out.println(lastTimeMap.get("confirmMonth")); // System.out.println("begin=="+getLastMonthBegin(0)); // System.out.println("end=="+getLastMonthEnd(0)); Map yesterdayOfTDay = DateUtil.getYesterdayOfTDay(9); System.out.println("begin=="+yesterdayOfTDay.get("yesterdayOfTBegin")); System.out.println("end=="+yesterdayOfTDay.get("yesterdayOfTEnd")); } /** * yyyy-MM-dd HH:mm:ss */ public static final String DEF_DATETIME_FMT = "yyyy-MM-dd HH:mm:ss"; }
  • 你可能感兴趣的:(Core,JAVA,Java,Web)