博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 工具类 DateUtil
阅读量:4841 次
发布时间:2019-06-11

本文共 10388 字,大约阅读时间需要 34 分钟。

提取了一些在开发过程中可能会用到的日期相关的函数作为工具类。供大家參考:

/** * 日期操作工具类. *  * @author shimiso */public class DateUtil {	private static final String FORMAT = "yyyy-MM-dd HH:mm:ss";	public static Date str2Date(String str) {		return str2Date(str, null);	}	public static Date str2Date(String str, String format) {		if (str == null || str.length() == 0) {			return null;		}		if (format == null || format.length() == 0) {			format = FORMAT;		}		Date date = null;		try {			SimpleDateFormat sdf = new SimpleDateFormat(format);			date = sdf.parse(str);		} catch (Exception e) {			e.printStackTrace();		}		return date;	}	public static Calendar str2Calendar(String str) {		return str2Calendar(str, null);	}	public static Calendar str2Calendar(String str, String format) {		Date date = str2Date(str, format);		if (date == null) {			return null;		}		Calendar c = Calendar.getInstance();		c.setTime(date);		return c;	}	public static String date2Str(Calendar c) {// yyyy-MM-dd HH:mm:ss		return date2Str(c, null);	}	public static String date2Str(Calendar c, String format) {		if (c == null) {			return null;		}		return date2Str(c.getTime(), format);	}	public static String date2Str(Date d) {// yyyy-MM-dd HH:mm:ss		return date2Str(d, null);	}	public static String date2Str(Date d, String format) {// yyyy-MM-dd HH:mm:ss		if (d == null) {			return null;		}		if (format == null || format.length() == 0) {			format = FORMAT;		}		SimpleDateFormat sdf = new SimpleDateFormat(format);		String s = sdf.format(d);		return s;	}	public static String getCurDateStr() {		Calendar c = Calendar.getInstance();		c.setTime(new Date());		return c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"				+ c.get(Calendar.DAY_OF_MONTH) + "-"				+ c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE)				+ ":" + c.get(Calendar.SECOND);	}	/**	 * 获得当前日期的字符串格式	 * 	 * @param format	 * @return	 */	public static String getCurDateStr(String format) {		Calendar c = Calendar.getInstance();		return date2Str(c, format);	}	// 格式到秒	public static String getMillon(long time) {		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(time);	}	// 格式到天	public static String getDay(long time) {		return new SimpleDateFormat("yyyy-MM-dd").format(time);	}	// 格式到毫秒	public static String getSMillon(long time) {		return new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS").format(time);	}				/*	 输入的是String,格式诸如20120102。实现加一天的功能,返回的格式为String。诸如20120103	 */	public static String stringDatePlus(String row) throws ParseException{		String year=row.substring(0, 4);		String month=row.substring(4,6);		String day=row.substring(6);		String date1=year+"-"+month+"-"+day;		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");		Date startDate=sdf.parse(date1);		Calendar cd = Calendar.getInstance();  		cd.setTime(startDate);		cd.add(Calendar.DATE, 1);		String dateStr =sdf.format(cd.getTime());		String year1=dateStr.substring(0,4);		String month1=dateStr.substring(5,7);		String day1=dateStr.substring(8);		return year1+month1+day1;	}		/*	 输入的是String,格式诸如20120102。实现减一天的功能,返回的格式为String,诸如20120101	 */	public static String stringDateDecrease(String row) throws ParseException{		String year=row.substring(0, 4);		String month=row.substring(4,6);		String day=row.substring(6);		String date1=year+"-"+month+"-"+day;		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");		Date startDate=sdf.parse(date1);		Calendar cd = Calendar.getInstance();  		cd.setTime(startDate);		cd.add(Calendar.DATE, -1);		String dateStr =sdf.format(cd.getTime());		String year1=dateStr.substring(0,4);		String month1=dateStr.substring(5,7);		String day1=dateStr.substring(8);		return year1+month1+day1;	}		/*	 输入的格式为String,诸如20120101。返回的格式为String,诸如2012-01-01	 */	public static String stringDateChange(String date)	{		if(date.length()=="20120101".length()){			String year=date.substring(0, 4);			String month=date.substring(4,6);			String day=date.substring(6);			return year+"-"+month+"-"+day;		}else{			return date;		}					}	/**	 * 日期向后推一天	 * @param date 格式:20120101 	 * @return  20120102	 */	public static String tonextday(String date){		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-1, day+1);		Date newdate = calendar.getTime();		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");		String da = format.format(newdate);		return da;	}	public static void main(String[] args) {		new StringHandle().previousWeekEndDayByDate("20130109");	}	/**	 * 获取当前日期上一周的開始日期 (周日)	 */	public static String previousWeekByDate(String date) {		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-1, day);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    if(1 == dayWeek) {	    	cal.add(Calendar.DAY_OF_MONTH, -1);	    }	    cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一	    int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//依据日历的规则。给当前日期减往星期几与一个星期第一天的差值 	    cal.add(Calendar.WEEK_OF_YEAR, -1);	    String imptimeBegin = sdf.format(cal.getTime());//	    System.out.println("所在周星期日的日期:"+imptimeBegin);	    return imptimeBegin;	}			/**	 * 获取当前日期上一周的结束日期 (周六)	 */	public static String previousWeekEndDayByDate(String date) {		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-1, day);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    if(1 == dayWeek) {	    	cal.add(Calendar.DAY_OF_MONTH, -1);	    }	    cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天。按中国的习惯一个星期的第一天是星期一	    int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s));	    cal.add(Calendar.WEEK_OF_YEAR, -1);	    String imptimeBegin = sdf.format(cal.getTime());//	    System.out.println("星期六的日期:"+imptimeBegin);	    return imptimeBegin;	}			/**	 * 获取当前日期当前一周的開始日期 (周日)	 */	public static String getCurrentWeekFirstDayByDate(String date) {		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-1, day);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    if(1 == dayWeek) {	    	cal.add(Calendar.DAY_OF_MONTH, -1);	    }	    cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天。按中国的习惯一个星期的第一天是星期一	    int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-s);//依据日历的规则。给当前日期减往星期几与一个星期第一天的差值 	  	    String imptimeBegin = sdf.format(cal.getTime());	  //  System.out.println("所在周星期日的日期:"+imptimeBegin);	    return imptimeBegin;	}	/**	 * 获取当前日期当前一周的结束日期 (周六)	 */	public static String getCurrentWeekEndDayByDate(String date) {		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-1, day);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    if(1 == dayWeek) {	    	cal.add(Calendar.DAY_OF_MONTH, -1);	    }	    cal.setFirstDayOfWeek(Calendar.SUNDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一	    int s = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天	    cal.add(Calendar.DATE, cal.getFirstDayOfWeek()+(6-s));	  	    String imptimeBegin = sdf.format(cal.getTime());	    return imptimeBegin;	}		/** * 返回上一个月的第一天 * @param date20120304 * @return  20120201 */	public static String previousMonthByDate(String date) {		// TODO Auto-generated method stub		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-2, 1);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    String imptimeBegin = sdf.format(cal.getTime());//	    System.out.println(imptimeBegin);		return imptimeBegin;	}		/**	 * 返回下一个月的第一天	 * @param date20120304	 * @return  20120401	 */	public static String nextMonthByDate(String date) {		// TODO Auto-generated method stub		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month, 1);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    String imptimeBegin = sdf.format(cal.getTime());//	    System.out.println(imptimeBegin);		return imptimeBegin;	}/** * 返回当前月的第一天  * @param date20120103 * @return 20120101 */	public static String getCurrentMonthFirstDayByDate(String date) {		int year = Integer.parseInt(date.substring(0,4));		int month = Integer.parseInt(date.substring(4,6));		int day = Integer.parseInt(date.substring(6));		Calendar calendar = Calendar.getInstance();		calendar.set(year, month-1, 1);		Date newdate = calendar.getTime();		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd"); 	    Calendar cal = Calendar.getInstance();	    cal.setTime(newdate);	    String imptimeBegin = sdf.format(cal.getTime());		return imptimeBegin;	}	}

转载于:https://www.cnblogs.com/hrhguanli/p/5091557.html

你可能感兴趣的文章
Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块
查看>>
hdu 5452 Minimum Cut 树形dp
查看>>
perf4j @Profiled常用写法
查看>>
配置的热更新
查看>>
ios view的frame和bounds之区别(位置和大小)
查看>>
USB小白学习之路(11) Cy7c68013A驱动电路设计注意事项(转)
查看>>
Luogu 2530 化工厂装箱员
查看>>
自定义webUI实例
查看>>
用NSAttributedString实现简单的图文混排
查看>>
多语境的操作
查看>>
SNS营销——网商成功之道
查看>>
jqgrid 加载时第一页面只显示多少条数据
查看>>
magic模块 :Exception Value:failed to find libmagic. Check your installation
查看>>
C#小游戏(文字对战游戏)
查看>>
COGS2314. [HZOI 2015] Persistable Editor
查看>>
关于dubbo+shiro导致dubbo无法注入到Realm的问题解决方案
查看>>
Solr添加paoding分词器
查看>>
charles 抓包 (一)
查看>>
隐藏电池栏,遮罩层
查看>>
ES6学习之Iterator和For...of循环
查看>>