How to use @DateTimeFormat to format Date Time request param in SpringBoot RestAPI Examples

springboot-restapi-datetimeformat-feature-image

[no_toc]
In the tutorial, we show how to work with DateTime request param in SpringBoot RestAPI using @DateTimeFormat.

@DateTimeFormat with DateTime Request Param

@DateTimeFormat is used to declare a field or method parameter should be formatted as a date or time.

We can use @DateTimeFormat with ISO date time pattern, or custom format pattern string:

– Common ISO enum value: DATE, TIME, DATE_TIME

  • DATE: yyyy-MM-dd, example 2019-03-28
  • TIME: HH:mm:ss.SSSXXX, example 01:30:00.000-05:00
  • DATE_TIME: yyyy-MM-dd'T'HH:mm:ss.SSSXXX, example 2019-03-28T01:30:00.000+07:00

Example @DateTimeFormat with @RequestParam in Spring RestAPI:

@GetMapping("/date/v1")
public String dateTimeApiV1(
		@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date,
		@RequestParam("localdate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate localdate, 
		@RequestParam("localdatetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localdatetime) {

– Custom format pattern string:

Example:

@GetMapping("/date/v2")
public String dateTimeApiV2(
		@RequestParam("date") @DateTimeFormat(pattern="yyyy.MM.dd") Date date,
		@RequestParam("localdate") @DateTimeFormat(pattern="yyyy.MM.dd") LocalDate localdate, 
		@RequestParam("localdatetime") @DateTimeFormat(pattern="yyyy.MM.dd HH:mm:ss") LocalDateTime localdatetime) {

Practice

Create SpringBoot project

We create a SpringBoot project as below:

D:\gkz\article\SpringBoot-DateTimeFormat-RestAPI

– Dependency:


	org.springframework.boot
	spring-boot-starter-web

Create DateTime RestAPIs

RestAPIs.java:

package com.ozenero.springboot.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class RestAPIs {

	@GetMapping("/date/v1")
	public String dateTimeApiV1(
			@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date,
			@RequestParam("localdate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate localdate, 
			@RequestParam("localdatetime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime localdatetime) {
		
		System.out.println(date);
		System.out.println(localdate);
		System.out.println(localdatetime);
		
		return "Done"; 
	}
	
	@GetMapping("/date/v2")
	public String dateTimeApiV2(
			@RequestParam("date") @DateTimeFormat(pattern="yyyy.MM.dd") Date date,
			@RequestParam("localdate") @DateTimeFormat(pattern="yyyy.MM.dd") LocalDate localdate, 
			@RequestParam("localdatetime") @DateTimeFormat(pattern="yyyy.MM.dd HH:mm:ss") LocalDateTime localdatetime) {
		
		System.out.println(date);
		System.out.println(localdate);
		System.out.println(localdatetime);
		
		return "Done"; 
	}	
}

Run & Check Results

Run SpringBoot project ->
– Request 1 with ISO format pattern:

SpringBoot-RestAPI-DateTimeFormat-project-structure-request-iso-pattern

-> Logs in SpringBoot app:

Fri Aug 23 07:00:00 GMT+07:00 2019
2019-09-12
2019-10-29T01:30

– Request 2 with Custom format pattern:

SpringBoot-RestAPI-DateTimeFormat-project-structure-request-custom-pattern

-> Logs in SpringBoot app:

Fri Aug 23 00:00:00 GMT+07:00 2019
2019-09-12
2019-10-29T12:01:23

Sourcecode

SpringDateTimeFormatExamples

Conclusion

We had learned how to use @DateTimeFormat to format Date Time request param in SpringBoot RestAPI with:

  • ISO date time pattern
  • Custom date time format pattern string

Happy Learning! See you later!

43 thoughts on “How to use @DateTimeFormat to format Date Time request param in SpringBoot RestAPI Examples”

  1. 248389 631171I wish I had a dime for every bad write-up Ive read lately. I also wish other writers had your talent and style. Thank you. 881073

  2. 328227 203486Wonderful beat ! I wish to apprentice although you amend your internet site, how can i subscribe for a weblog site? The account aided me a appropriate deal. I had been slightly bit acquainted of this your broadcast provided bright clear concept 11508

  3. 70593 564711Hi there! I simply want to give a huge thumbs up for the good data you can have appropriate here on this post. I will likely be coming again to your weblog for a lot more soon. 145928

  4. 44908 959044Most reliable human being messages, nicely toasts. are already provided gradually during the entire wedding celebration and therefore are anticipated to be extremely laid back, humorous and as properly as new all at once. very best man speech 17018

  5. 726949 470017Youre so cool! I dont suppose Ive learn anything like this before. So good to discover any person with some authentic thoughts on this topic. realy thank you for starting this up. this internet site is something that is wanted on the internet, someone with a little bit originality. helpful job for bringing something new towards the internet! 64821

  6. 866740 216057What a outstanding viewpoint, nonetheless is just not produce every sence by any means discussing this mather. Just about any technique thanks and also i had try and discuss your post directly into delicius but it surely appears to be an problem in your blogging is it possible you need to recheck this. thank you just as before. 618005

  7. 133677 976315Naturally I like your web-site, nonetheless you need to have to check the spelling on several of your posts. Numerous of them are rife with spelling problems and I find it very silly to inform you. On the other hand I will certainly come once again again! 836742

  8. 378883 124564This is the suitable blog for anybody who needs to seek out out about this subject. You notice so considerably its virtually laborious to argue with you (not that I truly would wantHaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Excellent stuff, just excellent! 198603

  9. 227828 226328Hey. Very good internet internet site!! Man .. Excellent .. Fantastic .. Ill bookmark this internet internet site and take the feeds alsoI am happy to locate so considerably beneficial information here within the write-up. Thanks for sharing 971856

  10. 47550 871209You will notice several contrasting points from New york Weight reduction eating program and every one 1 may possibly be helpful. The initial point will probably be authentic relinquishing on this excessive. shed weight 634669

  11. 565010 235274Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone! 403327

  12. 358544 827544Be the precise blog should you have wants to learn about this topic. You comprehend considerably its almost onerous to argue to you (not that I personally would needHaHa). You undoubtedly put a new spin for a topic thats been discussing for some time. Good stuff, simply nice! 502198

Leave a Reply

Your email address will not be published. Required fields are marked *