关闭

java 日期工具类

454人阅读 评论(0) 收藏 举报

写了一下针对时间日期的处理工具类,方便以后使用。

上代码

package dada.test.date;

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

public class DateUtil {

	public static int DATE_FORMAT = DateFormat.MEDIUM;
	public static Locale DATE_LOCALE = Locale.SIMPLIFIED_CHINESE;
	/**
	 * 得到当前日期.
	 * Creation date: (2012-3-22)
	 * @return java.lang.String
	 */
	public static String getCurrentDatetime(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}
	
	/**
	 * 得到当前日期.
	 * Creation date: (2001-7-16 13:56:17)
	 * @return java.lang.String
	 */
	public static String getCurrentDatetime(String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date());
	}
	
	/**
	 * Change date format.
	 * From: yyyy-MM-dd   To: yyyyMMdd
	 * @param oldValue String
	 * @return String
	 */
	public static String changeDateFormat(String oldValue) {
		DateFormat df = DateFormat.getDateInstance(DATE_FORMAT, DATE_LOCALE);
		Date date = null;
		try {
			date = df.parse(oldValue);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}
	
	/**
	 * 将日期转换为字符串
	 * 格式为yyyy-MM-dd
	 * @param aDate java.util.Date
	 * @return String
	 */
	public static String formatDate(java.util.Date aDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(aDate);
	}
	
	/**
	 * 将字符串转化为日期
	 * 格式为yyyy-MM-dd
	 * @param aDate java.util.Date
	 * @return String
	 */
	public static Date toDate(String dateString) throws  ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.parse(dateString);
	}
	
	/**
	 * 得到当前月.
	 * Creation date: (2001-7-16 13:56:17)
	 * @return java.lang.String
	 */
	public static String getCurrentMonth() {
		SimpleDateFormat df = new SimpleDateFormat("MM");
		return df.format(new Date());
	}
	
	/**
	 * 得到当前季度.
	 * Creation date: (2001-7-16 13:56:17)
	 * @return java.lang.String
	 */
	public static int getCurrentQuarter() {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(new Date());
		int month = c1.get(Calendar.MONTH)+1;
		int quarter = 1;
		if(month<=3){
			quarter = 1;
		}else if(month>3&&month<=6){
			quarter = 2;
		}else if(month>6&&month<=9){
			quarter = 3;
		}else{
			quarter = 4;
		}
		
		return quarter;
	}
	
	
	/**
	 * 得到当前年.
	 * Creation date: (2001-7-16 13:56:17)
	 * @return java.lang.String
	 */
	public static String getCurrentYear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy");
		return df.format(new Date());
	}
	

	/**
	 * 获取当前月的第一天
	 * @return java.lang.String
	 */
	public static String getFirstDateOfMonth() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-");
		return df.format(new Date())+"01";
	}
	
	/**
	 * 获取当前月的最后一天
	 * @return java.lang.String
	 */
	public static int getLastDateOfMonth() {
		Calendar c1 = Calendar.getInstance();
		c1.setTime(new Date());
		/*try {
			c1.setTime(toDate("2011-02-12"));
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
		return c1.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	/**
	 * 获取上个月的第一天
	 * @return String
	 */
	public static String getFirstDateOfLastMonth() {
		Calendar calendar = Calendar.getInstance();
//		try {
//			calendar.setTime(toDate("2012-01-12"));
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -1);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-");
		String s = df.format(calendar.getTime());
		s += "01";
		return s;
	}
	
	/**
	 * 获取与当前日期相差若干天的日期
	 * @return java.lang.String
	 * @param dif
	 */
	public static String getDate(int dif) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_YEAR, dif);
		return formatDate(calendar.getTime());
	}
	
	/**
	 * 两个日期间相差的天数
	 * * @return int
	 * @param date1 java.lang.String
	 * @param date2 java.lang.String
	 */
	public static long calBetweenDays(String date1, String date2) throws Exception{
		DateFormat df = DateFormat.getDateInstance(DATE_FORMAT,DATE_LOCALE);
		long d1 = df.parse(date1).getTime();
		long d2 = df.parse(date2).getTime();
		return (d1-d2)/1000 / 60 / 60 / 24;
	}
	
	/**
	 * 得到星期几
	 * 注意:星期日是0 ,星期一为1,星期二为2,以此类推 
	 * @param date
	 * @return
	 * create by dada
	 */
	public static int getWeekDay(Date date){
			Calendar c1 = Calendar.getInstance();
			c1.setTime(new Date());
			int week = c1.get(Calendar.DAY_OF_WEEK)-1;
			return week;
	}
	
	/**
	 * 验证日期的合法性
	 * @param date String
	 * @return boolean
	 */
	public static boolean isRightDate(String date) {
		boolean flag = true;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		sdf.setLenient(false);
		//System.out.println(sdf.isLenient());
		try {
			Date date2 = sdf.parse(date);
			//System.out.println(formatDate(date2));
		} catch (ParseException e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
	
	
	public static void main(String[] args) throws Exception {
		System.out.println(getCurrentDatetime());
		System.out.println(getCurrentDatetime("yyyy-MM-dd"));
		System.out.println(getCurrentDatetime("yyyy-MM-dd hh:mm:ss"));
		System.out.println(changeDateFormat("2012-12-21"));
		System.out.println(formatDate(new Date()));
		System.out.println(getCurrentMonth());
		System.out.println(getCurrentYear());
		System.out.println(getFirstDateOfMonth());
		System.out.println(toDate("2012-02-12"));
		System.out.println(getLastDateOfMonth());
		System.out.println("当前季度:"+getCurrentQuarter());
		System.out.println(getFirstDateOfLastMonth());
		System.out.println(getDate(-90));
		System.out.println(calBetweenDays("2012-03-12","2012-02-12"));
		System.out.println(isRightDate("1996-13-3"));
		System.out.println(getWeekDay(toDate("2012-03-26")));
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:267841次
    • 积分:2482
    • 等级:
    • 排名:第14872名
    • 原创:28篇
    • 转载:72篇
    • 译文:0篇
    • 评论:16条
    文章分类
    最新评论