【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);
	}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM