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

Ads

 
 

Share on Google+Share on Google+

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

Hi,

How to convert current date to mm dd yyyy format in Java? Share me the program for using in my project.

Thanks

Ads
View Answers

March 2, 2016 at 4:30 PM


Hi,

Please find the source code for converting current date into MM-dd-yyyy format.

Here is full code:

package net.roseindia;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
 * This program converts current date into 
 *  MM-dd-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 mm-dd-yyyy
            Date currentDate = new Date();
            DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
            //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