How to convert current date to dd mm yyyy format in Java?

Ads

 
 

Share on Google+Share on Google+

Java Coder
How to convert current date to dd mm yyyy format in Java?
1 Answer(s)      a year and 3 months ago
Posted in : Java Beginners

Hi,

How to convert current date to dd mm yyyy format in Java?

In my program I have to display the current date in the dd-mm-yyyy format to user on the website.

How to achieve the conversion of current date into this desired format?

Thanks

Ads
View Answers

March 2, 2016 at 4:04 PM


Hi,

Here is the source code for formatting the current date into dd-mm-yyyy format:

package net.roseindia;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
 * This program converts current date into 
 *  dd-mm-yyyy format. You can also change the format
 *  to convert the current date into desired format
 */
public class CurrentDateToString {
    public static void main(String[] args) {
        try {
            //Current date to dd-mm-yyyy
            Date currentDate = new Date();
            DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
            String strCurrentDate = df.format(currentDate);
            System.out.println("Date is " + strCurrentDate);
        } catch (Exception e) {
            System.out.println("Exception :" + e);
        }

    }
}

Thanks


Ads