Categories

Tools

bitdefender

1&1 Web Hosting

A simple method to convert a date from MySQL format to the Italian format and vice versa


Sometimes, it is necessary to convert the format of a date returned by the MySQL database in a different format, such as Italian. The following example shows a simple method to convert a date from MySQL format to the Italian format and vice versa.

public class ConvertDateFormat {
    public static void main(String[] args)
    {
        String mysqlDate = "2014-05-01";
        String italianDate = "16/08/2015";
	
	System.out.println("MySQL -> Italian");
	System.out.println("MySQL date is: " + mysqlDate);
	System.out.println("Italian date is: " + convertDateToItalianFormat(mysqlDate));
	System.out.println();
	System.out.println("Italian -> MySQL");
	System.out.println("Italian date is: " + italianDate);
	System.out.println("MySQL date is: " + convertDateToMySQLFormat(italianDate));
    }
    
    private static String convertDateToItalianFormat(String date) {
        String year = date.substring(0, 4);
        String month = date.substring(5, 7);
        String day = date.substring(8, 10);
        String convertsDate = day + "/" + month + "/" + year;
        return convertsDate;
    }
    
    private static String convertDateToMySQLFormat(String date) {
        String day = date.substring(0, 2);
	String month = date.substring(3, 5);	
	String year = date.substring(6, 10);
        
        String convertsDate = year + "-" +  month + "-" + day;
        return convertsDate;
    }
}

Result:

>java -cp . ConvertDateFormat
MySQL -> Italian
MySQL date is: 2014-05-01
Italian date is: 01/05/2014

Italian -> MySQL
Italian date is: 16/08/2015
MySQL date is: 2015-08-16
>Exit code: 0

Posted in Java by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.