This Java Utility is developed for converting the Date formats where the input may be String of one among the following date types and the output will be one among the following date type that the user can specify.
Date Types:
------------
YYYYMMDD, MMDDYYYY, DDMMYYYY,
DD-MM-YYYY,DD.MM.YYYY,DD/MM/YYYY,
MM-DD-YYYY,MM.DD.YYYY,MM/DD/YYYY,
YYYY-MM-DD,YYYY.MM.DD,YYYY/MM/DD
Date Types:
------------
YYYYMMDD, MMDDYYYY, DDMMYYYY,
DD-MM-YYYY,DD.MM.YYYY,DD/MM/YYYY,
MM-DD-YYYY,MM.DD.YYYY,MM/DD/YYYY,
YYYY-MM-DD,YYYY.MM.DD,YYYY/MM/DD
public static String changeDateFormat(String fromType,String toType,String fromValue) { String outputString = fromValue; if(fromType != toType){ outputString = parseInput(fromType, fromValue,toType); } return outputString; } public static String parseInput(String fromType,String fromValue,String toType ){ StringBuffer buffer = new StringBuffer(); int strDay = 0; int strMonth = 0; int strYear = 0; if(fromType.equals("YYYYMMDD")){ strDay = Integer.parseInt(fromValue.substring(6, 8)); strMonth = Integer.parseInt(fromValue.substring(4, 6)); strYear = Integer.parseInt(fromValue.substring(0, 4)); }else if(fromType.equals("MMDDYYYY")){ strMonth = Integer.parseInt(fromValue.substring(0, 2)); strDay = Integer.parseInt(fromValue.substring(2, 4)); strYear = Integer.parseInt(fromValue.substring(4, 8)); }else if(fromType.equals("DDMMYYYY")){ strDay = Integer.parseInt(fromValue.substring(0, 2)); strMonth = Integer.parseInt(fromValue.substring(2, 4)); strYear = Integer.parseInt(fromValue.substring(4, 8)); }else if(fromType.equals("YYYY-MM-DD") || fromType.equals("YYYY.MM.DD") || fromType.equals("YYYY/MM/DD")){ strDay = Integer.parseInt(fromValue.substring(8, 10)); strMonth = Integer.parseInt(fromValue.substring(5, 7)); strYear = Integer.parseInt(fromValue.substring(0, 4)); }else if(fromType.equals("MM-DD-YYYY")|| fromType.equals("MM.DD.YYYY")|| fromType.equals("MM/DD/YYYY") ){ strMonth = Integer.parseInt(fromValue.substring(0, 2)); strDay = Integer.parseInt(fromValue.substring(3, 5)); strYear = Integer.parseInt(fromValue.substring(6, 10)); }else if(fromType.equals("DD-MM-YYYY") || fromType.equals("DD.MM.YYYY") || fromType.equals("DD/MM/YYYY")){ strDay = Integer.parseInt(fromValue.substring(0, 2)); strMonth = Integer.parseInt(fromValue.substring(3, 5)); strYear = Integer.parseInt(fromValue.substring(6, 10)); } buffer = prepareOutputBuffer(strDay,strMonth,strYear,toType); return buffer.toString(); } public static StringBuffer prepareOutputBuffer(int strDay,int strMonth,int strYear,String toType){ StringBuffer buffer = new StringBuffer(); if(toType.equalsIgnoreCase("YYYYMMDD")){ buffer.append(strYear); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); }else if(toType.equals("MMDDYYYY")){ if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append(strYear); }else if(toType.equals("DDMMYYYY")){ if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append(strYear); }else if(toType.equals("YYYY-MM-DD") ){ buffer.append(strYear); buffer.append("-"); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("-"); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); }else if(toType.equals("YYYY.MM.DD") ){ buffer.append(strYear); buffer.append("."); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("."); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); }else if(toType.equals("YYYY/MM/DD")){ buffer.append(strYear); buffer.append("/"); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("/"); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); }else if(toType.equals("MM-DD-YYYY")){ if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("-"); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append("-"); buffer.append(strYear); }else if(toType.equals("MM.DD.YYYY")){ if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("."); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append("."); buffer.append(strYear); }else if(toType.equals("MM/DD/YYYY")){ if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("/"); if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append("/"); buffer.append(strYear); } else if(toType.equals("DD/MM/YYYY")){ if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append("/"); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("/"); buffer.append(strYear); } else if(toType.equals("DD-MM-YYYY")){ if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append("-"); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("-"); buffer.append(strYear); }else if(toType.equals("DD.MM.YYYY")){ if(strDay <=9 ) buffer.append("0"+strDay); else buffer.append(strDay); buffer.append("."); if(strMonth <=9 ) buffer.append("0"+strMonth); else buffer.append(strMonth); buffer.append("."); buffer.append(strYear); } return buffer; }