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

Ads
Ads

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


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

Hi,

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

Thanks

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



July 21, 2017 at 3:50 AM


Hi,

This example is very good. Congrats for providing such a good example.

Thanks


Ads