日期工具类DateUtisl-获取(日、周、月、季、年)起止时间

日期工具类,可以对日期进行格式化,获取日、周、月、季、年的起止时间
 
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static String[] getBegainAndEndTime(Date date, String mode) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date[] dates = getBegainAndEndDate(date, mode);
		
		return new String[]{format.format(dates[0]), format.format(dates[1])};
	}
	
	public static Date[] getBegainAndEndDate(Date date, String mode) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		Date begin = new Date();
		Date end = new Date();
		
		if (mode == null || mode.equalsIgnoreCase("日")
				|| mode.equalsIgnoreCase("day")) {
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			begin = calendar.getTime();
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			calendar.set(Calendar.MILLISECOND, 999);
			end = calendar.getTime();
		} else if (mode.equalsIgnoreCase("周") || mode.equalsIgnoreCase("week")) {
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			begin = calendar.getTime();
			calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			calendar.add(Calendar.DAY_OF_WEEK, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			calendar.set(Calendar.MILLISECOND, 999);
			end = calendar.getTime();
		} else if (mode.equalsIgnoreCase("月") || mode.equalsIgnoreCase("month")
				|| mode.equalsIgnoreCase("0")) {
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			begin = calendar.getTime();
			calendar.set(Calendar.DAY_OF_MONTH, calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH));
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			calendar.set(Calendar.MILLISECOND, 999);
			end = calendar.getTime();
		} else if (mode.equalsIgnoreCase("季")
				|| mode.equalsIgnoreCase("season")) {
			int month = calendar.get(Calendar.MONTH) + 1;
			if(month % 3 == 0){//季度结束月 6(3,4,5)
				calendar.set(Calendar.MONTH, month - 3);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				begin = calendar.getTime();
				calendar.set(Calendar.MONTH, month - 1);
				calendar.set(Calendar.DAY_OF_MONTH, calendar
						.getActualMaximum(Calendar.DAY_OF_MONTH));
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				end = calendar.getTime();
			}else if(month % 3 == 1){//季度起始月 4(3,4,5)
				calendar.set(Calendar.MONTH, month - 1);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				begin = calendar.getTime();
				calendar.set(Calendar.MONTH, month + 1);
				calendar.set(Calendar.DAY_OF_MONTH, calendar
						.getActualMaximum(Calendar.DAY_OF_MONTH));
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				end = calendar.getTime();
			}else if(month % 3 == 2){//季度中间月 5(3,4,5)
				calendar.set(Calendar.MONTH, month - 2);
				calendar.set(Calendar.DAY_OF_MONTH, 1);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
				calendar.set(Calendar.MILLISECOND, 0);
				begin = calendar.getTime();
				calendar.set(Calendar.MONTH, month);
				calendar.set(Calendar.DAY_OF_MONTH, calendar
						.getActualMaximum(Calendar.DAY_OF_MONTH));
				calendar.set(Calendar.HOUR_OF_DAY, 23);
				calendar.set(Calendar.MINUTE, 59);
				calendar.set(Calendar.SECOND, 59);
				calendar.set(Calendar.MILLISECOND, 999);
				end = calendar.getTime();
			}
		} else if (mode.equalsIgnoreCase("年") || mode.equalsIgnoreCase("year")
				|| mode.equalsIgnoreCase("1")) {
			calendar.set(Calendar.DAY_OF_YEAR, 1);
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			begin = calendar.getTime();
			calendar.set(Calendar.DAY_OF_YEAR, calendar
					.getActualMaximum(Calendar.DAY_OF_YEAR));
			calendar.set(Calendar.HOUR_OF_DAY, 23);
			calendar.set(Calendar.MINUTE, 59);
			calendar.set(Calendar.SECOND, 59);
			calendar.set(Calendar.MILLISECOND, 999);
			end = calendar.getTime();
		}

		return new Date[] {begin, end};
	}


	public static String format(Date date) {
		String result = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		result = format.format(date);
		return result;
	}
	
	public static String format(Date date, String pattern) {
		String result = "";
		if(null == pattern){
			pattern = "yyyy-MM-dd HH:mm:ss";
		}
		if(date == null){
			date = new Date();
			date.setTime(0);
		}
		
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		result = format.format(date);
		return result;
	}

	public static Date parse(String str) {
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;

	}

	public static String format(String str) {
		Date date = parse(str);
		String result = "";
		result = format(date);

		return result;
	}

	public static Date changeDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String newDateStr = sdf.format(date) + " 17:00:00";
		return getStringToDate(newDateStr);
	}
	
	public static String parse(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String newDateStr = sdf.format(date);
		return sdf.format(date);
	}

	// public static String changeDate(Date date) {
	// SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	// String newDateStr = sdf.format(date)+" 17:00:00";
	// return newDateStr;
	// }
	public static Date getStringToDate(String s) {

		Timestamp ts = Timestamp.valueOf(s);
		return ts;
	}

	public static void main(String[] args) {
		String[] dates = getBegainAndEndTime(new Date(), "month");
		System.out.println(dates[0] + " -- " + dates[1]);
		System.out.println(format(null, null));
	}
}


 

你可能感兴趣的:(Date,String,calendar,null,import,工具)