How to get today's date in java in mm/dd/yyyy format?

Ads
 
Tutorials   

How to get today's date in java in mm/dd/yyyy format?

In this tutorial I will show you the code to get today's date in Java in mm/dd/yyyy format.

In this tutorial I will show you the code to get today's date in Java in mm/dd/yyyy format.

Today's Date in MM/dd/yyyy format in Java - How to get today's date in java in mm/dd/yyyy format?

In this tutorial we are going to show you some examples of getting the current date in Java in MM/dd/yyyy format. Here MM represents the month part of the date in Java and the month part we can get using the MM while formatting the date.

Now first of all we will see how we can get the current date in Java?

Getting Current date in Java

There are many ways you can get current date in Java. Here are few methods of getting the current date in Java:

  1. Using java.util.Date class
  2. Using java.time.LocalDate class
  3. Using import java.util.Calendar class

Here is the complete example code:


package net.roseindia.date;

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
/*
 * Getting Current Date in Java
 * https://www.roseindia.net
 */
public class CurrentDateExample {

	public static void main(String[] args) {
		// Using java.util.Date class
		Date currentDate = new Date();
		System.out.println("Current Date: " + currentDate);

		// Using java.time.LocalDate
		LocalDate currentDate2 = LocalDate.now();
		System.out.println("Current Date: " + currentDate2);

		// Using import java.util.Calendar class
		Calendar calendar = Calendar.getInstance();
		Date currentDate3 = calendar.getTime();
		System.out.println("Current Date: " + currentDate3);

	}

}

Here is the output of the program:

Getting Current Date in Java

Here is the output of the program:


Current Date: Sat Jul 10 10:36:07 IST 2021
Current Date: 2021-07-10
Current Date: Sat Jul 10 10:36:08 IST 2021

So, we can use java.util.Date, java.time.LocalDate and java.util.Calendar classes to get the current date in Java.

Converting today's date into MM/dd/yyyy format

Now we will explain you the code to convert today's date into MM/dd/yyyy format. The MM/dd/yyyy format formats the date into 12/01/2021 format and this format is mostly used in United States. This format is Month-Day-Year with leading zeros format of the date e.g. 12/01/2021.

We will use SimpleDateFormat format java.util.Date, while DateTimeFormatter for formatting the java.time.LocalDate. Here is the code form formatting today's date into MM/dd/yyyy format:


// Formatting Date in MM/dd/yyyy format
// Date formatter
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String currentStrDate = dateFormat.format(currentDate);
System.out.println("Current Date: " + currentStrDate);

DateTimeFormatter dateTimeFormat = DateTimeFormatter
		.ofPattern("MM/dd/yyyy");
currentStrDate = currentDate2.format(dateTimeFormat);
System.out.println("Current Date: " + currentStrDate);

currentStrDate = dateFormat.format(currentDate3);
System.out.println("Current Date: " + currentStrDate);

Here is the update complete code:


package net.roseindia.date;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/*
 * Getting Current Date in Java
 * https://www.roseindia.net
 */
public class CurrentDateExample {
	public static void main(String[] args) {
		// Using java.util.Date class
		Date currentDate = new Date();
		System.out.println("Current Date: " + currentDate);

		// Using java.time.LocalDate
		LocalDate currentDate2 = LocalDate.now();
		System.out.println("Current Date: " + currentDate2);

		// Using import java.util.Calendar class
		Calendar calendar = Calendar.getInstance();
		Date currentDate3 = calendar.getTime();
		System.out.println("Current Date: " + currentDate3);

		// Formatting Date in MM/dd/yyyy format
		// Date formatter
		DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
		String currentStrDate = dateFormat.format(currentDate);
		System.out.println("Current Date: " + currentStrDate);

		DateTimeFormatter dateTimeFormat = DateTimeFormatter
				.ofPattern("MM/dd/yyyy");
		currentStrDate = currentDate2.format(dateTimeFormat);
		System.out.println("Current Date: " + currentStrDate);

		currentStrDate = dateFormat.format(currentDate3);
		System.out.println("Current Date: " + currentStrDate);

	}
}

Here is the screen shot of output of the program:

Format Today's date in MM/dd/yyyy format

If you run the program you will get following output (in your case it will be your today's date):


Current Date: Sat Jul 10 10:36:07 IST 2021
Current Date: 2021-07-10
Current Date: Sat Jul 10 10:36:08 IST 2021
Current Date: 07/10/2021
Current Date: 07/10/2021
Current Date: 07/10/2021

In this tutorial we explained you the code to get today's date and then format it into MM/dd/yyyy format. Java comes with rich date API which can be used to develop program that required data calculations.

Here is the list of Date and Time examples in Java programming language:

Ads

Ads