【dateFormatSymbols】JAVA特殊日期格式转换

 

记录:特殊日期格式转换,如将yyyyMMdd转为01MAY2019

	
    public static final String DATE_VIP_FORMAT = "yyyyMMdd";

    public static String format(Date targetDate, String formatStr){
		if (targetDate == null || StringUtils.isBlank(formatStr)){
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(formatStr);
		return format.format(targetDate);
	}

	public static Date parse(String date, String pattern){
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		try {
			return simpleDateFormat.parse(date);
		} catch (ParseException e) {
			//blocker解决
			logger.error("parse date error for input String {}",date);
		}
		return null;
	}

	public static String formatVipDateStr(Date date) {
		return format(date, DATE_VIP_FORMAT);

	}
	public static Date parseVipDateStr(String date) {
		return parse(date, DATE_VIP_FORMAT);

	}

	/**
	 * 将01MAY2019 转换为yyyyMMdd
	 */
	public static String  getVipStr(String date){
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
			DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
			dateFormatSymbols.setShortMonths(new String[]{"JAN", "FEB", "MAR"
				, "APR", "MAY", "JUN"
				, "JUL", "AUG", "SEP"
				, "OCT", "NOV", "DEC"});
			dateFormat.setDateFormatSymbols(dateFormatSymbols);


			Date parse = dateFormat.parse(date);
			return formatVipDateStr(parse);
		} catch (ParseException e) {
			logger.error("parse VIP date error for input String {}",date);
		}
		return null;
	}
	/**
	 * 将yyyyMMdd转为01MAY2019
	 */
	public static String  getVipFmt(String dateStr){
		try {

			//获取date对象
			Date date = parseVipDateStr(dateStr);


			SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMMyyyy", Locale.ENGLISH);
			DateFormatSymbols dateFormatSymbols = new DateFormatSymbols();
			dateFormatSymbols.setShortMonths(new String[]{"JAN", "FEB", "MAR"
				, "APR", "MAY", "JUN"
				, "JUL", "AUG", "SEP"
				, "OCT", "NOV", "DEC"});
			dateFormat.setDateFormatSymbols(dateFormatSymbols);
			String format = dateFormat.format(date);
			return format;
		} catch (Exception e) {
			logger.error("parse VIP date error for input String {}",dateStr);
		}
		return null;
	}
	public static void main(String[] args) {
		String vipStr = getVipFmt("20190503");
		System.out.println(vipStr);
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值