实际企业项目中用到的时间处理的基础方法(2)

获取时间

/**
	 * 
	 * @Description:获取当前时间,格式yyyy-MM-dd HH:mm:ss
	 * @param type
	 *            (0,当前时间,1秒,2分,3小时,4天,5月,6年)
	 * @param time
	 *            (当前时间前后时间点)
	 * @return
	 * @version:v1
	 * @date:2017年10月24日 下午3:14:18
	 */
	public static String getStringDate(String type, int time) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		if ("1".equals(type)) {
			c.add(Calendar.SECOND, time);
		} else if ("2".equals(type)) {
			c.add(Calendar.MINUTE, time);
		} else if ("3".equals(type)) {
			c.add(Calendar.HOUR, time);
		} else if ("4".equals(type)) {
			c.add(Calendar.DATE, time);
		} else if ("5".equals(type)) {
			c.add(Calendar.MONTH, time);
		} else if ("6".equals(type)) {
			c.add(Calendar.YEAR, time);
		}
		date = c.getTime();
		String dateString = df.format(date);
		return dateString;
	}

判断是否润年

public static boolean isLeapYear(String ddate) {

		/**
		 * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
		 * 3.能被4整除同时能被100整除则不是闰年
		 */
		Date d = strToDateLong1(ddate);
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(d);
		int year = gc.get(Calendar.YEAR);
		if ((year % 400) == 0)
			return true;
		else if ((year % 4) == 0) {
			if ((year % 100) == 0)
				return false;
			else
				return true;
		} else
			return false;
	}

获取两个时间内所有月份

public static List<String> getMonthBetween(String minDate, String maxDate)
			throws ParseException {
		ArrayList<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M");// 格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(sdf.parse(minDate));
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

		max.setTime(sdf.parse(maxDate));
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

		Calendar curr = max;
		while (curr.after(min)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, -1);
		}
		return result;
	}

获取指定的前一天日期

public static String getSpecifiedDayBefore(String specifiedDay)
			throws ParseException {
		Calendar c = Calendar.getInstance();
		Date date = new SimpleDateFormat("yy/MM/dd").parse(specifiedDay);
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);

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

两个日期之间的天数

public static int daysBetween(String smdate, String bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值