WHAT'S NEW?

Converting date from Indian Format(dd-mm-yyyy) to SQL Format(yyyy-mm-dd)

While working in projects it is one of the tricky task to convert the date from and to the various formats.


Especially for developers in India where date is majorly used in format DD-MM-YYYY face a difficulty of changing the date to and from the SQL format.

I am going to make the use of method split() of class String. In case you have never used the methods you can check the working here.

The actual code goes as follow

class DateWorker
{
public static String convert(String date)
{
String[] dateArray=date.split("-");
String convertedDate=dateArray[2]+"-"+dateArray[1]+"-"+dateArray[0];
return convertedDate;
}
}


public class ConvertDate
{
public static void main(String[] args)
{
String normalDate="23-05-2015";
String sqlDate=DateWorker.convert(normalDate);
System.out.println(sqlDate);

String normal=DateWorker.convert(sqlDate);
System.out.println(normal);
}
}


One method of DateWorker is enough for both the conversion as both the conversion are nothing but inversions.

Try the code and you will understand the rest. 

0 comments:

Post a Comment