DateTime Formatting



The course is part of this learning path

Start course

In this course, we will talk about DateTime Class. 

Learning Objectives

  • LocalDate Class
  • LocalTime Class
  • LocalDateTime Class
  • Period Class
  • DateTime Formatting

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this lesson, we'll talk about the DateTimeFormatter class. So far, we've learned to print the date and time on the console screen, but if you notice the date and time were printed on the console screen in a standard format. So, what should we do if we want to change the format of the date and time?

In this case, we'll use the DateTimeFormatter class. In the example on the slide, when we apply our own format to the standard date, we can get the date 3 Dec 2011. There are different methods that we basically use in this formatting process. First, we can use the predefined fixed formats like ISO_LOCAL_DATE. Secondly, we can use a pattern that we determine ourselves such as yyyy-MMM-dd. Third, we can use the localized style like short or medium. We'll make some examples of these shortly. Finally, this class is in the java.time.format package. This class is immutable and thread-safe like other classes. If you're ready, let's switch to Eclipse and get some practice. First, I'll create a new class. I right click on the "DateTimeExamplepackage" and select the 'New Class' options. The class name can be "DateTimeFormattingExample." And I'll check the checkbox for the main method and click the 'Finish' button. Okay, first let's create a date. LocalDate myDate = LocalDate.of. The year can be 2022, the month can be 2, the day can be 20.

Okay. Now, I'll create a time. LocalTime myTime = LocalTime.of. The hour can be 10, the minute can be 30, and the second can be 25. Okay. Finally, let's create a DateTime object. LocalDateTime myDateTime = LocalDateTime.of (myDate, myTime). Now, let's print the myDateTimeObject on the console. S.out ("default date-time; "+myDateTime). Now, let's use the predefined ISO_DateTime constant. DateTimeFormatter myConstantFormatter = DateTimeFormatter.ISO. As you can see, there are a lot of constants. If you format the date, you must select the date format. If you format the time, you must select the time format, otherwise you get the Runtime Exception error. Pay attention to this. I'll use the DateTime formatting. So here, I'll select the ISO_LOCAL_DATE_TIME. Let's apply this format to the myDateTime object and print it on the console. S.out ("ISO date-time;" + myDateTime.format(myConstantFormatter)). Now, let's run and see. As you can see, the default format style is the same as the ISO DateTime format. You can apply other formats to the time and date objects. Now, I'll show you the second format style, the localized style.

I will create another formatting object. DateTimeFormatter myLocalizedFormatter = DateTimeFormatter.ofLocalized. As you can see, there are three types of this format style, for the date, for the time and for the DateTime. If you format the date, you must select the date format. If you format the time, you must select the time format, otherwise you get the Runtime error. Pay attention to this. To show you the Runtime error, first, I will select the ofLocalized date style and in parentheses, I write (Formatstyle.MEDIUM). You can test the other options such as short or long or full styles. Let's apply this format to the myTime object and print it on the console. S.out ("localized style medium;" + myTime.format(myLocalizedformatter)). Notice, the type of format as date, but here, we apply it to the time. Let's run the code and test it. As you can see, we get a Runtime exception. It says 'Unsupported temporal type exception'. This is one of the points you should pay attention to when formatting. If you're going to format the date, you should use the date format. If you're going to format the time, you should use the time format. If you use the date format for the time or the time format for the date, you will get a Runtime error instead of a compile error. Pay attention to this. Now, I'll fix the format to the date time. Also, this will be myDateTime. Now, let's run it and see. As you can see, the medium style also looks like this. Finally, let's look at the third method, the custom formatting.

I'll create another formatting object, DateTimeFormatter myCustomformatter = DateTimeFormatter.ofPattern. Inside the double quotes, we'll write the format style. For example, let's print the year first. Letter y represents the year. I'm putting four ys here. Having four ys means that the year is shown as four digits. If you put two letters, y, the year will also be displayed as two digits. Let's show the month after the year. I'm going to put four M here in capital letters. If you use a single letter M, the month will be represented as a number and the first nine months will be single digits. If you use two M letters, this time the month will be a number again but the first nine letters will be printed with two digits, that is, with a zero to the left. If you use three M, this time the month name will be written as text but as an abbreviation. For example, Feb for the month February. If you use four M, this time the month name will be as written as text and the full name will be clearly written. You can test them one by one yourself. Finally, let's set the day. I'm putting two d here. If you want, let's separate them with a dot, not a space. For this, I'll put a dot between them. Yes, now let's write the time part. I only want hours and minutes on the screen, therefore, I'll use two h letters to represent the hour. I'll also separate the hours and minutes with a colon. And I'll use two m letters to represent the minute. Yes, formatting is okay. Now, let's apply this to the myDateTime object.

S.out ("custom date-time;" + myDateTime.format (myCustomFormatter)). Also, we can write like that myCustomFormatter.format (myDateTime). This syntax is also valid. Okay, now let's run the code. As you can see, the custom format is applied to the date and time. Okay, I think you got it. Let's take a short break here. I'll see you in the next video.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics