得到当前日期内N天的所有日期,并过滤几种格式的时间为正常日期格式

 

需要距离今天30天内过去所有日期:

getDaysList(-30);

需要距离今天30天内未来所有日期:

getDaysList(30);

/**
	 * 得到所有日期
	 * 
	 * @param num 距离今天多少天,正数返回未来日期,负数返回过去日期
	 */
	private static List<String> getDaysList(int num) {
		List<String> dateList = new ArrayList<>();
		List<String> datePathList = new ArrayList<>();
		for (int i = 0; i < 90; i++) {
			String onedate = plusDay2(i);
			dateList.add(onedate);
		}
		for (int i = 0; i < dateList.size(); i++) {
			Integer year = Integer.parseInt(dateList.get(i).split("-")[0]);
			Integer month = Integer.parseInt(dateList.get(i).split("-")[1]);
			Integer day = Integer.parseInt(dateList.get(i).split("-")[2]);
			String date= year +"-"+ month +"-" + day  ;
            date=formatString(date+DateFormatUtil.YYYY_MM_DD);
			datePathList.add(date);
		}
		return datePathList;
	}
/**
	 * 当前日期加上天数后的日期
	 * 
	 * @param num
	 *            为增加的天数
	 * @return
	 */
	public static String plusDay2(int num) {
		Date d = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DATE, -num);// num为增加的天数,可以改变的
		d = ca.getTime();
		String enddate = format.format(d);
		return enddate;
	}
/**
	 * 日期格式化,例如 DateFormat.formatDate(mydate, DateFormat.YYYY_MM_DD)
	 * 
	 * @param date
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static String formatString(String date, int type) throws Exception {
		if (date == null) {
			return "";
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern(DateFormatUtil.getDateConvertString(type));
		Date a=dateFormat.parse(date);
		return dateFormat.format(a);
	}
public class DateFormatUtil {
/**
	 * 日期格式常量对应的字符串
	 * 
	 * @param type
	 * @return
	 */
	private static String getDateConvertString(int type) {
		String strFormatString = "";
		switch (type) {
		case DateFormatUtil.YYYY_MM_DD:
			strFormatString = "yyyy-MM-dd";
			break;
		case DateFormatUtil.YYYY_MM_DD_HH_MM_SS:
			strFormatString = "yyyy-MM-dd HH:mm:ss";
			break;
		case DateFormatUtil.YYYYMMDDHHMMSS:
			strFormatString = "yyyyMMddHHmmss";
			break;

		case DateFormatUtil.YYYYMMDD:
			strFormatString = "yyyyMMdd";
			break;
		case DateFormatUtil.YYYY:
			strFormatString = "yyyy";
			break;
		case DateFormatUtil.MM:
			strFormatString = "MM";
			break;
		case DateFormatUtil.DD:
			strFormatString = "dd";
			break;
		default:
			strFormatString = "yyyy-MM-dd";
			break;
		}
		return strFormatString;
	}
public static final int YYYY_MM_DD = 1;// yyyy-MM-dd
}

 如果时间格式为以下几种格式:

        String days1="2月09日 16:23";
        String days2="2018年11月28日 18:12";
        String days3="2018-11-13 19:01 转赞人数超过10";
        String days4="1月15日 12:21 转赞人数超过50";
        String days5="37分钟前";
        String days6="今天08:16";
        String days7="今天 11:21 转赞人数超过40";

调用getPublishDate(),可以得到例如"2019-02-01"标准日期格式字符串。

/**
	 * 得到时间格式字符串
	 * 
	 * @param contentDate
	 * @return
	 */
	private String getPublishDate(String contentDate) {
		String year = null;
		String month = null;
		String day = null;
		//得到当前时间的年月日
		String[] strNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString().split("-");
		year = strNow[0];
		month = strNow[1];
		day = strNow[2];
		if (contentDate.contains("日")) {
			if (contentDate.contains("年") && contentDate.contains("月") && contentDate.contains("日")) {
				String date = getDate(contentDate, "年", "月");
				year = date.trim().substring(date.trim().indexOf("年") - 4, date.trim().indexOf("年"));
				month = date.trim().substring(date.trim().indexOf("年") + 1, date.trim().indexOf("月"));
				day = date.trim().substring(date.trim().indexOf("月") + 1, date.trim().length());
			} else if (contentDate.contains("月") && contentDate.contains("日")) {
				String date = getDate(contentDate, "月");
				month = date.split("月")[0];
				day = date.split("月")[1];
			} else {
				day = contentDate.trim().substring(0, contentDate.trim().indexOf("日"));
			}
		} else if (contentDate.contains("-")) {
			String date = getDate(contentDate, "-", "-");
			year = date.split("-")[0];
			month = date.split("-")[1];
			day = date.split("-")[2];
		} else {
			
		}
		String dateNumber=Integer.parseInt(year)+"-"+Integer.parseInt(month)+"-"+Integer.parseInt(day);
		try {
			return DateFormatUtil.formatString(dateNumber, DateFormatUtil.YYYY_MM_DD);
		} catch (Exception e) {
			return null;
		}
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmlcq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值