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

ConvertDate

 //if you are given 02102022 then return 2nd Oct 2022

 

package abc;

public class ConvertDate { 

       public static void main(String[] args) {           

             String givenWord = "02102022";

           

             //Find day

             int ddOfMonth = Integer.parseInt( (Character.toString(givenWord.charAt(0))).concat(Character.toString(givenWord.charAt(1))));

             String dayFormat = null;

                    switch(ddOfMonth) {

                    case 01 : dayFormat = "1st";

                    break ;

                    case 02 : dayFormat = "2nd";

                    break ;

                    case 03 : dayFormat = "3rd";

                    break ;

                    case 04 : dayFormat = "4th";

                    break ;

                    case 05 : dayFormat = "5th";

                    break ;

                    case 06 : dayFormat = "6th";

                    break ;

                    case 07 : dayFormat = "7th";

                    break ;

                    case 08 : dayFormat = "8th";

                    break ;

                    case 09 : dayFormat = "9th";

                    break ;

                    default : dayFormat = ddOfMonth+"th";

                    break ;

                    }

                   

                    //Find Month

                    String monthFormat = null;

                    int givenMonth  = Integer.parseInt( (Character.toString(givenWord.charAt(2))).concat(Character.toString(givenWord.charAt(3))));

                    switch(givenMonth) {

                    case 01 : monthFormat = "Jan";

                    break ;

                    case 02 : monthFormat = "Feb";

                    break ;

                    case 03 : monthFormat = "March";

                    break ;

                    case 04 : monthFormat = "April";

                    break ;

                    case 05 : monthFormat = "May";

                    break ;

                    case 06 : monthFormat = "June";

                    break ;

                    case 07 : monthFormat = "July";

                    break ;

                    case 08 : monthFormat = "Aug";

                    break ;

                    case 09 : monthFormat = "Sep";

                    break ;

                    case 10 : monthFormat = "Oct";

                    break ;

                    case 11 : monthFormat = "Nov";

                    break ;

                    case 12 : monthFormat = "Dec";

                    break ;

                    }

                   

                    //Find Year

                    String givenYear = (Character.toString(givenWord.charAt(4))).concat(Character.toString(givenWord.charAt(5))).concat(Character.toString(givenWord.charAt(6))).concat(Character.toString(givenWord.charAt(7)));

                   

                    System.out.println("The converted date = "+dayFormat+" "+monthFormat+" "+givenYear);

             }

}

 

OutPut:

The converted date = 2nd Oct 2022

For 22112022

The converted date = 22th Nov 2022

No comments:

Post a Comment