zoukankan      html  css  js  c++  java
  • 【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);
    	}
  • 相关阅读:
    jQuery为啥要提供一个load()方法?
    某大学程序设计竞赛
    【Error】JavaWeb: 严重: Failed to initialize end point associated with ProtocolHandler ["http-bio-8080"]
    form表单和表格
    AJAX入门---DOM操作HTML
    设计模式学习01—单例模式
    Spring MVC 数据验证——validate注解方式
    HOW TO: How to import UUID function into Postgre 9.3
    STM8S 串口应用 UART2 STM8S105
    微信公众平台应用开发框架sophia设计不足(1)
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054052.html
Copyright © 2011-2022 走看看