Calendar date in YYYY-MM-DD format in Java
Java Date
encapsulates the current time and date. The Date class does this with the help of two constructors - Date()
and Date(long millisec)
constructor.
We use Date()
the constructor to initialize the object with the current time and date. On the other hand, Date(long millisec)
the constructor takes the number of milliseconds since midnight on January 1, 1970 as a parameter.
But the output we get is not yyyy-MM-dd
in format. This article will show how we can generate output in this format.
We use the get method in Java date()
to get the current date and time. For this, we use toString()
the Date object with the get method. Look at the following example to understand it.
import java.util.Date;
public class DateExample {
public static void main(String args[]) {
// Create a date object
Date date_of_today = new Date();
// Use toString() to print the date
System.out.println(date_of_today.toString());
}
}
Output:
Mon Jan 10 09:58:43 UTC 2022
This example uses date_of_today
the DDate object to instantiate and toString()
the date method to print the date. Notice that we get output that contains the day, month, day and year, and 小时:分钟:秒
the time in a formatted format.
This poses a problem when we use two or more dates. To remove the names of the dates and times from this output, we can use printf()
the method in two ways. The first uses the symbols %
and $
.
See the example below.
import java.util.Date;
public class DateExample {
public static void main(String args[]) {
// Create a new object
Date date_of_today = new Date();
// Display the date
System.out.printf("%1$s %2$tB %2$td, %2$tY", "Date:", date_of_today);
}
}
Output:
Date: January 10, 2022
The second method is to use <flag
. Let's look at the same example again.
import java.util.Date;
public class DateExample {
public static void main(String args[]) {
// Create a new object
Date date_of_today = new Date();
// display the date
System.out.printf("%s %tB %<te, %<tY", "Date:", date_of_today);
}
}
Output:
Date: January 10, 2022
Note that in these examples, the time is omitted. But we still don't get yyyy-MM-dd
output in the format of . Let's see how to deal with this.
Convert date to YYYY-MM-DD
Format in Java
Java has a java.time
package. java.time
Instead of using the Date class to work with date and time, we can use the package. Let's look at an example.
// import the java.time package
import java.time.LocalDate;
public class DemoOfDate {
public static void main(String[] args) {
// create an object for date
LocalDate date_of_today = LocalDate.now();
// Display the date
System.out.println(date_of_today);
}
}
Output:
2022-01-10
In this example, we use java.time
the package and LocalDate
the class. This time we get yyyy-MM-dd
the output in the format. java.time
The package has four main classes.
-
LocalDate
- Ityyyy-MM-dd
gives date as output in year, month, day or format. -
LocalTime
- ItHH-mm-ss-ns
gives the output in the form of time and nanoseconds. -
LocalDateTime
- Ityyyy-MM-dd-HH-mm-ss-ns
gives output in terms of date and time. -
DateTimeFormatter
- This acts as a formatter for date-time objects, both for displaying and parsing them.
Notice that we have used the class in the above example LocalDate
. Also, we have used now()
the method. The method in java now()
is LocalTime
a method of the class. It gets the current time in the default time zone from the system clock.
grammar:
public static LocalTime now()
We are not now()
passing any parameters in the method. Another way is yyyy-MM-dd
to get the output in format.
YYYY-MM-DD
Formatting
a date in Java
To change the calendar date to yyyy-MM-dd
a format, we can also use the concept of formatting. It makes working with date and time more flexible.
For this, we use the method java.time
from the package ofPattern()
. The various ways in which we can use ofPattern()
the method to get the output are as follows.
-
yyyy-MM-dd
- This gives the output as2022-01-25
-
dd/MM/yyyy
- This gives the output as25/01/2022
-
dd-MMM-yyyy
- This gives the output as25/01/2022
-
E, MMM dd yyyy
- This gives the output asTue, Jan 25 2022
Let’s look at an example.
// import the LocalDateTime class
import java.time.LocalDateTime;
// import the DateTimeFormatter class
import java.time.format.DateTimeFormatter;
public class DemoOfDate {
public static void main(String[] args) {
LocalDateTime date_of_today = LocalDateTime.now();
System.out.println("Output before formatting: " + date_of_today);
DateTimeFormatter format_date_of_today = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = date_of_today.format(format_date_of_today);
System.out.println("Output after formatting: " + formattedDate);
}
}
Output:
Output before formatting: 2022-01-01T10:46:21.449669400
Output after formatting: 2022-01-01
Note that we can use formatting to get yyyy-MM-dd
output in . We can also move values in all of these formats.
For example, when we ofPattern()
pass a string in method yyyy-MM-dd
, we can change that string to yyyy-dd-MM
or dd-MM-yyyy
or in any other way as required. This applies to ofPattern()
all the formats provided by method.
We have used another approach here - format()
the method. format()
The method formats a string using the given locale, parameters and Java format.
If we do not specify a locale, format()
the method calls Locale.getDefault()
the method to get the default locale. format()
The method works printf()
the same way as the function.
SimpleDateFormat
Using Classes
in Java
To get yyyy-MM-dd
the output in format, we can use SimpleDateFormat
class. It provides methods to format (change date to string) and parse (change string to date) date and time in Java.
By inheriting from it, it can java.text.DateFormat
be used with the class. Let's see an example.
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatDemo {
public static void main(String[] args) {
Date date_of_today = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String stringDate = format.format(date_of_today);
System.out.println(stringDate);
}
}
Output:
2022-01-01
in conclusion
This article discusses how to change calendar date into yyyy-MM-dd
format in Java. It includes using java.time
the package, using toPattern()
the method and SimpleDateFormat
the class for formatting. The various methods are toString()
the and now()
methods.
We can get yyyy-MM-dd
the output in format and use it when it contains multiple dates.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Implementing a min-max heap in Java
Publish Date:2025/04/14 Views:53 Category:Java
-
In this article, we will implement a max heap and a min heap using PriorityQueue the class. We will also demonstrate inserting and removing elements from the heap. Introduction to Min-Max Heap in Java Heap is a tree-based data structure, wh
Implementing a Min Heap in Java
Publish Date:2025/04/14 Views:197 Category:Java
-
A min heap is a heap in which every internal node is less than or equal to the value of its child nodes. We will see in the following points how to implement a min heap with and without using a library. Minimal heap implementation in Java w
Increasing the heap space in Java
Publish Date:2025/04/14 Views:190 Category:Java
-
In Java, the heap space is mainly used for garbage collection and allocating memory for objects. A default heap space is allocated when JVM is installed on our machine, but it may be different. The following points show how we can increase
Detecting EOF in Java
Publish Date:2025/04/14 Views:91 Category:Java
-
In this tutorial, we will see how to while detect EOF( End OF File ) in Java using a loop. We will also discuss developing a program that continues reading content until it reaches the end of a file. From a programming perspective, EOF is a
Get resource URL and content in Java
Publish Date:2025/04/14 Views:97 Category:Java
-
getResource() This tutorial will demonstrate how to use the function to get the resource URL and read the resource file in Java . getResource() Use the function to get the resource URL in Java We will use the method in Java getResource() to
Getting the host name in Java
Publish Date:2025/04/14 Views:78 Category:Java
-
In this tutorial, we will see how to get the IP address and host name using Java API. InetAddress Get the host name in Java using The package java.net contains classes for handling the IP address and host name of the current machine InetAdd
Get the IP address of the current device in Java
Publish Date:2025/04/14 Views:53 Category:Java
-
An Internet Protocol (IP) address is an identifier for each device connected to a TCP/IP network. This identifier is used to identify and locate nodes in the middle of communication. The IP address format, such as 127.0.0.0, is a human-read
Generate a random double value between 0 and 1 in Java
Publish Date:2025/04/14 Views:153 Category:Java
-
This article will introduce three methods to generate double random values between 0 and 1 of primitive types. To demonstrate the randomness of the generated values, we will use a loop to generate ten random double values betwee
Setting the seed of a random generator in Java
Publish Date:2025/04/14 Views:62 Category:Java
-
A seed is a number or vector assigned to a pseudo-random generator to generate the desired sequence of random values. If we pass the same seed, it will generate the same sequence. We usually assign the seed as the system time. In this way,