java时间格式化整理

public static void main(String[] args) {
		
		String s = getFormatString("2019-06-24 23:12:03");
		s = getFormatString("2019/06/24 23:12:03");
		s = getFormatString("20190624 23:12:03");
		s = getFormatString("2019-06-24");
		s = getFormatString("2019/06/24");
		s = getFormatString("20190624");
		s = getFormatString("20sd0624");
		
		System.out.println(s);
		
	}
	
	public static String getFormatString(String date) {
		
		String result = "";
		try {
			
			if(date==null){
				result= "";
			}else if(date.contains("/") || date.contains("-")){
				date = date.replaceAll("/", "-");
				Date d = new SimpleDateFormat("yyyy-MM-dd").parse(date);
				result = new SimpleDateFormat("yyyy-MM-dd").format(d);
			}else{
				Date d = new SimpleDateFormat("yyyyMMdd").parse(date);
				result = new SimpleDateFormat("yyyy-MM-dd").format(d);
			}
		
		} catch (ParseException e) {
			result= "";
			e.printStackTrace();
		}
		return result;
	}
发布了132 篇原创文章 · 获赞 64 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qiuzhi__ke/article/details/94740498
今日推荐