Java util date format dd mm yyyy

Format Date to yyyy-MM-dd in java

In this post, we will see how to Format Date to yyyy-MM-dd in java.

There are multiple ways to format Date to yyyy-MM-dd in java. Let’s go through them.

Using DateTimeFormatter with LocalDate (Java 8)

To Format Date to yyyy-MM-dd in java:

  • Create a LocalDateTime object.
  • Use DateTimeFormatter.ofPattern() to specify the pattern yyyy-MM-dd
  • Call format() method on DateTimeFormatter and pass LocalDateTime object
  • Get result in String with date format yyyy-mm-dd .

Let’s see with the help of example:

Using SimpleDateFormat

To Format Date to yyyy-MM-dd in java:

  • Create Date object
  • Create SimpleDateFormat with pattern yyyy-MM-dd
  • Call format() method on SimpleDateFormat and pass Date object
  • Get result in String with date format yyyy-mm-dd.

Further reading:

How to get day name from date in java
Format LocalDateTime to String in Java

Using Calendar class

To Format Date to yyyy-MM-dd in java:

  • Use Calendar’s setTime() method to set current date
  • Use calendar class to get day, month and year using Calendar.get() methods.
  • Create format yyyy-MM-dd using String concatenation.

This is not a recommended approach as it does not specify format explicitly and is more error prone.

Читайте также:  Css темная тема автоматически

Let’s see with the help of example:

String yyyymmddFormat = ( year ) + «-» + ( ( month < 10 ) ? "0" + month : month ) + "-" + ( ( day < 10 ) ? "0" + day : day ) ;

Note that we have added 1 after getting month using calendar.get(Calendar.MONTH) because it returns month from 0 to 11. 0 denotes January and 11 denotes December.

That’s all about how to Format Date to yyyy-MM-dd in java.

Was this post helpful?

Share this

Author

Check if Date Is Between Two Dates in Java

Table of ContentsIntroductionDate & Local Date Class in JavaCheck if The Date Is Between Two Dates in JavaUsing the isAfter() and isBefore() Methods of the Local Date Class in JavaUsing the compareTo() Method of the Local Date Class in JavaUsing the after() and before() Methods of the Date Class in JavaUsing the compare To() Method […]

Find Difference Between Two Instant in Java

Table of ContentsWays to find difference between two Instant in JavaUsing Instant’s until() methodUsing ChronoUnit EnumUsing Duration’s between() method In this post, we will see how to find difference between two Instant in Java. Ways to find difference between two Instant in Java There are multiple ways to find difference between two Instant in various […]

Find Difference Between Two LocalDate in Java

Table of ContentsWays to find difference between two LocalDate in JavaUsing LocalDate’s until() methodUsing ChronoUnit EnumUsing Duration’s between() methodUsing Period’s between() methodFrequently asked questionsHow to find difference between two LocalDate in Java in Days?How to find difference between two LocalDate in Java in Years?How to find difference between two LocalDate in Java in Months?How to […]

Читайте также:  Collections and arraylist java

Get List of Weekday Names in Java

Table of ContentsUsing DateFormatSymbols’s getWeekdays() methodUsing DateFormatSymbols’s getShortWeekdays() method [For short names]Using DateFormatSymbols’s getWeekdays() with Locale In this post, we will see how to get list of weekday names in Java. Using DateFormatSymbols’s getWeekdays() method We can use DateFormatSymbols’s getWeekdays() to get list of weekday names in Java. It will provide complete weekday names like […]

Get List of Month Names in Java

Table of ContentsUsing DateFormatSymbols’s getMonth() methodUsing DateFormatSymbols’s getShortMonths() method [ For short names]Using DateFormatSymbols’s getMonth() with Locale In this post, we will see how to get list of months name in Java. Using DateFormatSymbols’s getMonth() method We can use DateFormatSymbols’s getMonth() to get list of months in Java. It will provide complete month names like […]

Get Unix Timestamp in Java

Table of ContentsWays to get unix timestamp in JavaUsing Instant classUsing System.currentTimeMillis()Using Date’s getTime() methodConvert Date to unix timestamp in JavaUsing Instant classUsing Date’s getTime() method In this post, we will get unix timestamp in Java. As per wikipedia Unix time is a system for describing a point in time. It is the number of […]

Источник

How do I format a date into dd/MM/yyyy?

Formatting how data should be displayed on the screen is a common requirement when creating a program or application. Displaying information in a good and concise format can be an added values to the users of the application. In the following code snippet we will learn how to format a date into a certain display format.

For these purposes we can utilize the DateFormat and SimpleDateFormat classes from the java.text package. We can easily format a date in our program by creating an instance of SimpleDateFormat class and specify to format pattern. Calling the DateFormat.format(Date date) method will format a date into a date-time string.

Читайте также:  All player java apps

You can see the details about date and time patters in the following link: Date and Time Patterns. Now, let’s see an example as shown in the code snippet below.

package org.kodejava.text; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateFormatExample < public static void main(String[] args) < Date date = Calendar.getInstance().getTime(); // Display a date in day, month, year format DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); String today = formatter.format(date); System.out.println("Today : " + today); // Display date with day name in a short format formatter = new SimpleDateFormat("EEE, dd/MM/yyyy"); today = formatter.format(date); System.out.println("Today : " + today); // Display date with a short day and month name formatter = new SimpleDateFormat("EEE, dd MMM yyyy"); today = formatter.format(date); System.out.println("Today : " + today); // Formatting date with full day and month name and show time up to // milliseconds with AM/PM formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a"); today = formatter.format(date); System.out.println("Today : " + today); >> 

Let’s view what we got on the console:

Today : 24/09/2021 Today : Fri, 24/09/2021 Today : Fri, 24 Sep 2021 Today : Friday, 24 September 2021, 09:36:32.724 AM 

A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏

Источник

Оцените статью