Java 获取固定格式的日期工具类 DateUtil

/*
 * Created on 2013.04.17
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.util;

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

/**
 * @author Hua
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class DateUtil {

	public static String getNowDateAndTime() {
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateformat.format(new Date());
	}

	public static String getNowTime() {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
		return dateformat.format(new Date());
	}

	public static String getNowHMS() {
		SimpleDateFormat dateformat = new SimpleDateFormat("HH:mm:ss");
		return dateformat.format(new Date());
	}

	public static String getNowYear() {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy");
		return dateformat.format(new Date());
	}

	public static String getNowMonth() {
		SimpleDateFormat dateformat = new SimpleDateFormat("MM");
		return dateformat.format(new Date());
	}

	public static String getNowDay() {
		SimpleDateFormat dateformat = new SimpleDateFormat("dd");
		return dateformat.format(new Date());
	}

	public static String getNowDate() {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		return dateformat.format(new Date());
	}

	public static String getBuinessDate() {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		return dateformat.format(new Date());
	}

	public static String getBuinessTime() {
		SimpleDateFormat dateformat = new SimpleDateFormat("H");
		return dateformat.format(new Date());
	}
	public static String getBuinessTimeExact() {
		SimpleDateFormat dateformat = new SimpleDateFormat("H:mm");
		return dateformat.format(new Date());
	}

	public static String getTomrrowDate() {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		Date tomrrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
		return dateformat.format(tomrrow);
	}

	public static String getYesterdayDate(String type, int num) {
		Date tomrrow = null;
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		if (type.equals(("add"))) {
			tomrrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * num);
		} else if (type.equals(("sub"))) {
			tomrrow = new Date(new Date().getTime() - 24 * 60 * 60 * 1000 * num);
		}

		return dateformat.format(tomrrow);
	}

	public static String getYesterdayDate(String date, String type, int num) {
		Date tomrrow = null;

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		try {
			Date temp = dateformat.parse(date);
			if (type.equals(("add"))) {
				tomrrow = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * num);
			} else if (type.equals(("sub"))) {
				tomrrow = new Date(temp.getTime() - 24 * 60 * 60 * 1000 * num);
			}
		} catch (ParseException e) {
			System.out.println(e.getMessage());
			return null;
		}

		return dateformat.format(tomrrow);
	}

	/**
	 * 在一个日期上,加减一定天数.返回一个新的日期
	 * 
	 * @param 日期字符串
	 * @param type日期类型:add
	 *            向前 sub向后
	 * @param 天数
	 * @return
	 */
	public static String getDate(String date, String type, int num) {
		Date tomrrow = null;

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		try {
			Date temp = dateformat.parse(date);
			if (type.equals(("add"))) {
				tomrrow = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * num);
			} else if (type.equals(("sub"))) {
				tomrrow = new Date(temp.getTime() - 24 * 60 * 60 * 1000 * num);
			}
		} catch (ParseException e) {
			System.out.println(e.getMessage());
			return null;
		}

		return dateformat.format(tomrrow);
	}
	
	/**
	 * 在一个日期上,加减一定天数.返回一个新的日期
	 * 
	 * @param 日期字符串
	 * @param type日期类型:add
	 *            向前 sub向后
	 * @param 天数
	 * @return
	 */
	public static String getForMatDate(String date, String type, int num) {
		Date tomrrow = null;

		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date temp = dateformat.parse(date);
			if (type.equals(("add"))) {
				tomrrow = new Date(temp.getTime() + 24 * 60 * 60 * 1000 * num);
			} else if (type.equals(("sub"))) {
				tomrrow = new Date(temp.getTime() - 24 * 60 * 60 * 1000 * num);
			}
		} catch (ParseException e) {
			System.out.println(e.getMessage());
			return null;
		}

		return dateformat.format(tomrrow);
	}


	/**
	 * 根据一个日期得到星期几
	 * 
	 * @param 日期
	 * @return
	 */
	public static String getWeekByDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int dayOfWeek = 0;
		String dayOfweekStr = "";
		dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		// 根据日期date得到是星期几
		switch (dayOfWeek) {
		case 1:
			dayOfweekStr = "日";
			break;
		case 2:
			dayOfweekStr = "一";
			break;
		case 3:
			dayOfweekStr = "二";
			break;
		case 4:
			dayOfweekStr = "三";
			break;
		case 5:
			dayOfweekStr = "四";
			break;
		case 6:
			dayOfweekStr = "五";
			break;
		case 7:
			dayOfweekStr = "六";
			break;
		}
		return dayOfweekStr;
	}

	/**
	 * 判断两个日期相隔多少天
	 * 
	 * @param 日期
	 * @return
	 */
	public static int getBetWeenDateNum(String startDate, String endDate) {

		int year1 = Integer.parseInt(startDate.substring(0, 4));
		int month1 = Integer.parseInt(startDate.substring(4, 6));
		int day1 = Integer.parseInt(startDate.substring(6, 8));

		int year2 = Integer.parseInt(endDate.substring(0, 4));
		int month2 = Integer.parseInt(endDate.substring(4, 6));
		int day2 = Integer.parseInt(endDate.substring(6, 8));

		Calendar c1 = Calendar.getInstance();
		c1.set(year1, month1 - 1, day1);
		Calendar c2 = Calendar.getInstance();
		c2.set(year2, month2 - 1, day2);

		int betWeenDays = (int) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / (1000 * 60 * 60 * 24));

		return betWeenDays;
	}
	
	/**
	 * 日期增加天数后得到新日期
	 * 
	 * @param 日期
	 * @return
	 */
	public static String getNewDate(String dateStr, int addDay) {
		
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd");
		
		Date date = null;
		try {
			date = dateformat.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DATE, addDay);
		date = c.getTime();
		
		String newDate = dateformat.format(date);
		
		return newDate;
	}
}

你可能感兴趣的:(Java)