java日期设置类

java日期设置类,第1张

java日期设置
public class DateUtil {
	
	public static final String FORMAT0 = "yyyy-MM-dd";
	public static final String FORMAT1 = "yyyyMMdd";
	public static final String FORMAT2 = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT3 = "yyyyMMddHHmmss";
    
    private static String[] DAYWEEK = {"周一","周二","周三","周四","周五","周六","周日"};
    
    
    public static String getWeek(String dateStr,String dateFmt, String weekPre) {
        
        Date dat = getDate(dateStr, dateFmt);
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(dat);
        int w=cal.get(java.util.Calendar.DAY_OF_WEEK)-1;
        String weekString = "";
        String wPre = null == weekPre?"星期":weekPre;
        switch (w) {
	        case 0:{
				weekString = "日";
				break;
			}
			case 1:{
				weekString = "一";
				break;
			}
			case 2:{
				weekString = "二";
				break;
			}
			case 3:{
				weekString = "三";
				break;
			}
			case 4:{
				weekString = "四";
				break;
			}
			case 5:{
				weekString = "五";
				break;
			}
			case 6:{
				weekString = "六";
				break;
			}
        }
		return wPre + weekString;
    }
    
    
	 public static String dayForWeek(String fmt, String pTime) {
		SimpleDateFormat format = new SimpleDateFormat(fmt);
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(pTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return DAYWEEK[dayForWeek-1];
	 }
	 
	 
	 public static int dayForWeekNum(String fmt, String pTime) {
		SimpleDateFormat format = new SimpleDateFormat(fmt);
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(pTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	 }
	 
	
	 public static int dayNumberForWeek(String fmt, String pTime) {
		SimpleDateFormat format = new SimpleDateFormat(fmt);
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(pTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	 }
	 
	 
	 public static int getCurrDay() {
		int retObj = -1;
    	Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        retObj = c.get(Calendar.DAY_OF_WEEK);
        if ( retObj > 1 ) {
        	retObj = retObj - 1;
        } else if ( retObj == 1 ) {
        	retObj = 7;
        }
    	return retObj;
	 }
	 
	 
	 public static int getCurrHour() {
			int retObj = -1;
	    	Calendar c = Calendar.getInstance();
	        c.setTime(new Date());
	        retObj = c.get(Calendar.HOUR_OF_DAY);
	    	return retObj;
	 }
	 
	 
	 public static int getCurrMinute() {
			int retObj = -1;
	    	Calendar c = Calendar.getInstance();
	        c.setTime(new Date());
	        retObj = c.get(Calendar.MINUTE);
	    	return retObj;
	 }
	 
	 
	 
    public static int getDayByDateAndFmt(String fmt, String date) {
    	int retObj = -1;
    	Calendar c = Calendar.getInstance();
        Date dat = null;
        try {
        	dat = new SimpleDateFormat(fmt).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(dat);
        retObj = c.get(Calendar.DAY_OF_WEEK);
        if ( retObj > 1 ) {
        	retObj = retObj - 1;
        } else if ( retObj == 1 ) {
        	retObj = 7;
        }
    	return retObj;
    }
    
    
    public static String getDayBefore(String format, int n) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - n);
        String dayBefore = new SimpleDateFormat(format).format(c.getTime());
        return dayBefore;
    }
    
    
    public static Date getDayMiniteBefore(String fmt, String dateTime, int n) {
        SimpleDateFormat format = new SimpleDateFormat(fmt);
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(dateTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) - n);
        //String dayBefore = new SimpleDateFormat(fmt).format(c.getTime());
        return c.getTime();
    }
    
    
    
    public static String getDayNext(String format, int n) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + n);
        String dayBefore = new SimpleDateFormat(format).format(c.getTime());
        return dayBefore;
    }
    
    
    public static String getTodayBeginTime(String format) {
   
    	Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
        String beginTime = new SimpleDateFormat(format).format(c.getTime());
        return beginTime;
    }
    
    
    public static String getTodayEndTime(String format) {
   
    	Calendar c = Calendar.getInstance();
    	c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
        String endTime = new SimpleDateFormat(format).format(c.getTime());
        return endTime;
    }

    
    public static String getNowDateByFmt(String fmt) {
        Date now = new Date();
        fmt = fmt.replace('Y','y');
        fmt = fmt.replace('D','d');
        //fmt = fmt.replace('S','s');
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        String nowDate = sdf.format(now);
        return nowDate;
    }
    
    
	public static Date getDate(String strTime,String timeFmt) {
		DateFormat fmt = new SimpleDateFormat(timeFmt);
		Date date = null;
		try {
			date = fmt.parse(strTime);
		} catch (ParseException e) {
		}
		return date;
	}
	
	
	public static String formatDay(String timeFmt, Date date) {
		DateFormat fmt = new SimpleDateFormat(timeFmt);
		return fmt.format(date);
	}
	
	public static Date formatDay2(String timeFmt, String date) {
		DateFormat fmt = new SimpleDateFormat(timeFmt);
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
    
    public static String getDateFormat0() {
        return dateFormate(FORMAT0, new Date());
    }
    
    
    public static String getDateFormat1() {
        return dateFormate(FORMAT1, new Date());
    }
    
    
    public static String getDateFormat2() {
        return dateFormate(FORMAT2, new Date());
    }
    
    
    public static String getDateFormat3() {
        return dateFormate(FORMAT3, new Date());
    }
    
    
    private static String dateFormate(String format, Date date) {
        DateFormat fmt = new SimpleDateFormat(format);
        return fmt.format(date);
    }
    
    
    public static int getNowYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }
    
    public static int getNowMonth() {
        return Calendar.getInstance().get(Calendar.MONTH) + 1;
    }
    
    public static int getNowDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }
    
    
    public static Integer getIntegerTimes(){
    	return Integer.valueOf(String.valueOf(Calendar.getInstance().getTimeInMillis()/1000));
    }
    
    
    public static String getDateFORMAT0Stirng(){
    	return DateUtil.FORMAT0;
    }
    
    public static String getDateFORMAT1Stirng(){
    	return DateUtil.FORMAT1;
    }
    
    public static String getDateFORMAT2Stirng(){
    	return DateUtil.FORMAT2;
    }
    
    public static String getDateFORMAT3Stirng(){
    	return DateUtil.FORMAT3;
    }
    
    
    public static String getSpecifiedDayBefore(String specifiedDay,int beforeDAy){ 
    	Calendar c = Calendar.getInstance(); 
    	Date date=null; 
    	try { 
    	date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay); 
    	} catch (ParseException e) { 
    	e.printStackTrace(); 
    	} 
    	c.setTime(date); 
    	int day=c.get(Calendar.DATE); 
    	c.set(Calendar.DATE,day-beforeDAy); 

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5576209.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存