자바 정규 각종 날짜 포맷 실현
10047 단어 자바
package com.st.test; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.regex.Pattern; public class DateFormatUtil { @SuppressWarnings ( "finally" ) public static String FormatDate(String dateStr){ HashMap dateRegFormat = new HashMap(); dateRegFormat.put( "^\\d{4}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D+\\d{1,2}\\D*$" , "yyyy-MM-dd-HH-mm-ss" ); //2014 3 12 13 5 34 ,2014-03-12 12:05:34,2014/3/12 12:5:34 dateRegFormat.put( "^\\d{4}\\D+\\d{2}\\D+\\d{2}\\D+\\d{2}\\D+\\d{2}$" , "yyyy-MM-dd-HH-mm" ); //2014-03-12 12:05 dateRegFormat.put( "^\\d{4}\\D+\\d{2}\\D+\\d{2}\\D+\\d{2}$" , "yyyy-MM-dd-HH" ); //2014-03-12 12 dateRegFormat.put( "^\\d{4}\\D+\\d{2}\\D+\\d{2}$" , "yyyy-MM-dd" ); //2014-03-12 dateRegFormat.put( "^\\d{4}\\D+\\d{2}$" , "yyyy-MM" ); //2014-03 dateRegFormat.put( "^\\d{4}$" , "yyyy" ); //2014 dateRegFormat.put( "^\\d{14}$" , "yyyyMMddHHmmss" ); //20140312120534 dateRegFormat.put( "^\\d{12}$" , "yyyyMMddHHmm" ); //201403121205 dateRegFormat.put( "^\\d{10}$" , "yyyyMMddHH" ); //2014031212 dateRegFormat.put( "^\\d{8}$" , "yyyyMMdd" ); //20140312 dateRegFormat.put( "^\\d{6}$" , "yyyyMM" ); //201403 dateRegFormat.put( "^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$" , "yyyy-MM-dd-HH-mm-ss" ); //13:05:34 dateRegFormat.put( "^\\d{2}\\s*:\\s*\\d{2}$" , "yyyy-MM-dd-HH-mm" ); //13:05 dateRegFormat.put( "^\\d{2}\\D+\\d{1,2}\\D+\\d{1,2}$" , "yy-MM-dd" ); //14.10.18( . . ) dateRegFormat.put( "^\\d{1,2}\\D+\\d{1,2}$" , "yyyy-dd-MM" ); //30.12( . ) dateRegFormat.put( "^\\d{1,2}\\D+\\d{1,2}\\D+\\d{4}$" , "dd-MM-yyyy" ); //12.21.2013( . . ) String curDate = new SimpleDateFormat( "yyyy-MM-dd" ).format( new Date()); DateFormat formatter1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); DateFormat formatter2; String dateReplace; String strSuccess= "" ; try { for (String key : dateRegFormat.keySet()) { if (Pattern.compile(key).matcher(dateStr).matches()) { formatter2 = new SimpleDateFormat(dateRegFormat.get(key)); if (key.equals( "^\\d{2}\\s*:\\s*\\d{2}\\s*:\\s*\\d{2}$" ) || key.equals( "^\\d{2}\\s*:\\s*\\d{2}$" )) { //13:05:34 13:05 dateStr = curDate + "-" + dateStr; } else if (key.equals( "^\\d{1,2}\\D+\\d{1,2}$" )) { //21.1 ( . ) dateStr = curDate.substring( 0 , 4 ) + "-" + dateStr; } dateReplace = dateStr.replaceAll( "\\D+" , "-" ); // System.out.println(dateRegExpArr[i]); strSuccess = formatter1.format(formatter2.parse(dateReplace)); break ; } } } catch (Exception e) { System.err.println( "----------------- :" +dateStr); throw new Exception( " " ); } finally { return strSuccess; } } public static void main(String[] args) { String[] dateStrArray= new String[]{ "2014-03-12 12:05:34" , "2014-03-12 12:05" , "2014-03-12 12" , "2014-03-12" , "2014-03" , "2014" , "20140312120534" , "2014/03/12 12:05:34" , "2014/3/12 12:5:34" , "2014 3 12 13 5 34 " , "201403121205" , "1234567890" , "20140312" , "201403" , "2000 13 33 13 13 13" , "30.12.2013" , "12.21.2013" , "21.1" , "13:05:34" , "12:05" , "14.1.8" , "14.10.18" }; for ( int i= 0 ;i
System.out.println(dateStrArray[i] + "------------------------------" .substring( 1 , 30 -dateStrArray[i].length())+ FormatDate(dateStrArray[i])); } } }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.