All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.seejoke.date.DateTimeUtil Maven / Gradle / Ivy

There is a newer version: 0.3
Show newest version
package com.seejoke.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @author Neason
 *
 */
public class DateTimeUtil {

	/**
	 * 获得指定日期的前一天
	 * 
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getDateBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

		String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayBefore;
	}

	/**
	 * 获得指定日期的后一天
	 * 
	 * @param specifiedDay
	 * @return
	 */
	public static String getDateAfter(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day + 1);

		String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		return dayAfter;
	}

	public static boolean isLastTime(String obTime, String referTime, String format) throws ParseException {
		boolean result = false;
		DateFormat df = new SimpleDateFormat(format);
		Date dt1 = df.parse(obTime);
		Date dt2 = df.parse(referTime);
		if (dt1.getTime() > dt2.getTime()) {
			result = true;
		}
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy