java 关于日期的一些常用惯例

1,获取当前毫秒数

两种方式:

方式一:new Date().getTime()

方式二:System.currentTimeMillis()

2,Date 转化为Timestamp

timestamp=new Timestamp(new Date().getTime());

3,格式化:

/***
	 * yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDateTime(Date date) {// format date ,such as
		SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddHHmmss);
		String formatTimeStr = null;
		if(StringUtil.isNullOrEmpty(date)){
			/*若没有传递参数,则默认为当前时间*/
			date=new Date();
		}
		if (date != null) {
			formatTimeStr = sdf.format(date);
		}
		return formatTimeStr;
	}
/***
	 * format : yyyy年MM月dd日 HH点mm分ss秒
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String formatTimestampZH(Timestamp timestamp) {
		if(StringUtil.isNullOrEmpty(timestamp)){
			/* 如果没有传参数timestamp,则默认为当前时间*/
			timestamp=new Timestamp(new Date().getTime());
		}
		SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS_ZH);
		return sdf.format(timestamp);
	}

4,获取指定天数之前的时间

public static Timestamp getTimestampBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return new Timestamp(now.getTimeInMillis());
	}
/***
	 * 
	 * @param d : 基准日期
	 * @param day : 几天前
	 * @return
	 * @throws ParseException
	 */
	public static java.util.Date getDateBefore(String d, int day) throws ParseException {
		java.util.Date date=getDate4Str(d);
		return getDateBefore(date, day);
	}
/***
	 * 
	 * @param d : 基准时间
	 * @param day
	 * @return
	 * @throws ParseException
	 */
	public static long getSecondBefore(String d, int day) throws ParseException {
		java.util.Date date=getDate4Str(d);
		return getDateBefore(date, day).getTime()/1000;
	}

	public static java.util.Date getDateBeforeMinute(Date d, int minutes) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) - minutes);
		return now.getTime();
	}

5,获取指定天数之后的时间

/***
	 * 
	 * @param d
	 *            :Base Date
	 * @param day
	 *            :Delayed days
	 * @return
	 */
	public static java.util.Date getDateAfter(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		return now.getTime();
	}

	public static java.util.Date getDateAfterByYear(Date d, int year) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.YEAR, now.get(Calendar.YEAR) + year);
		return now.getTime();
	}

	/***
	 * 以月为单位
	 * @param d
	 * @param month
	 * @return
	 */
	public static java.util.Date getDateAfterByMonth(Date d, int month) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);
		return now.getTime();
	}
	/***
	 * 以小时为单位
	 * @param d
	 * @param month
	 * @return
	 */
	public static java.util.Date getDateAfterByHour(Date d, int hour) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
		return now.getTime();
	}

6,Date 转化为Calendar

public static Calendar getCalendar(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

7,校验日期

/**
	 * 
	 * Determine whether date is valid, including the case of a leap year
	 * 
	 * @param date
	 *            YYYY-mm-dd
	 * @return
	 */
	public static boolean isDate(String date) {
		StringBuffer reg = new StringBuffer(
				"^((\\d{2}(([02468][048])|([13579][26]))-?((((0?");
		reg.append("[13578])|(1[02]))-?((0?[1-9])|([1-2][0-9])|(3[01])))");
		reg.append("|(((0?[469])|(11))-?((0?[1-9])|([1-2][0-9])|(30)))|");
		reg.append("(0?2-?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][12");
		reg.append("35679])|([13579][01345789]))-?((((0?[13578])|(1[02]))");
		reg.append("-?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))");
		reg.append("-?((0?[1-9])|([1-2][0-9])|(30)))|(0?2-?((0?[");
		reg.append("1-9])|(1[0-9])|(2[0-8]))))))");
		Pattern p = Pattern.compile(reg.toString());
		return p.matcher(date).matches();
	}

8,转化为cron 

/***
	 * convert Date to cron ,eg.  "0 06 10 15 1 ? 2014"
	 * @param date  : 时间点
	 * @return
	 */
	public static String getCron(java.util.Date  date){
		String dateFormat="ss mm HH dd MM ? yyyy";
		return formatDateByPattern(date, dateFormat);
	}

猜你喜欢

转载自hw1287789687.iteye.com/blog/2227047