http://www.technicalpage.net/search/label/SQL

convertDateFormat

 

// Convert date format such as: "24th Jul 1999" to "07-24-1999".

//The day might contain st, nd, rd,th etc. please take care of that

//The month contains only first 3 chars of the name, please take care of that

 

package test;

 

public class ConvertDateFormatUSING_Case {

 

       public static void main(String[] args) {

             String givenDate = "24th Jul 1999";

             String[] dateArray = givenDate.split(" ");

             String givenDay = dateArray[0];

             String givenMonth = dateArray[1];

             String givenYear = dateArray[2];

            

             String newDay = ConvertDateFormatUSING_Case.findNewDay(givenDay);

             String newMonth = ConvertDateFormatUSING_Case.findNewMonth(givenMonth);

             String newYear = givenYear;

            

             System.out.println("The converted date is = "+newDay+"-"+newMonth+"-"+newYear);

       }

 

       //--------------Convert Month---------------------

       public static String findNewDay(String givenDay1) {

             String newDay = "";

             switch (givenDay1) {

             case "1st": newDay = "01";

                    break;

 

             case "2nd":  newDay = "02";

                    break;

 

             case "3rd":  newDay = "03";

                    break;

 

             case "21st": newDay = "21";

                    break;

 

             case "22nd": newDay = "22";

                    break;

 

             case "23rd": newDay = "23";

                    break;

 

             case "31st": newDay = "31";

                    break;

 

             default: newDay = Character.toString(givenDay1.charAt(0)).concat(Character.toString(givenDay1.charAt(1)));

                    break;

             }

             return newDay;

 

       }

      

       //--------------Convert Month---------------------

       public static String findNewMonth(String givenMonth1) {

             String newMonth = "";

             switch (givenMonth1) {

             case "Jan": newMonth = "01";

                    break;

 

             case "Feb": newMonth = "02";

                    break;

 

             case "Mar": newMonth = "03";

                    break;

 

             case "Apr": newMonth = "04";

                    break;

 

             case "May": newMonth = "05";

                    break;

 

             case "Jun": newMonth = "06";

                    break;

 

             case "Jul": newMonth = "07";

                    break;

                   

             case "Aug": newMonth = "08";

                    break;

 

             case "Sep": newMonth = "09";

                    break;

 

             case "Oct": newMonth = "10";

                    break;

 

             case "Nov": newMonth = "11";

                    break;

 

             case "Dec": newMonth = "12";

                    break;

 

             }

             return newMonth;

 

       }

 

}

 Output:

The converted date is = 24-07-1999
 

No comments:

Post a Comment