×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: mauro larese
Added: Jul 2, 2022 4:43 PM
Views: 13
  1. package it.quofind.application.miscellanea;
  2.  
  3. import java.time.LocalDate;
  4. import java.time.LocalDateTime;
  5. import java.time.ZoneId;
  6. import java.time.format.DateTimeFormatter;
  7.  
  8. import java.util.Locale;
  9.  
  10. import org.springframework.format.annotation.DateTimeFormat;
  11.  
  12. import com.fasterxml.jackson.annotation.JsonFormat;
  13.  
  14. import io.swagger.v3.oas.annotations.media.Schema;
  15. import lombok.Data;
  16.  
  17.  
  18. /*
  19.  *   https://www.baeldung.com/java-datetimeformatter
  20.  *
  21.  * Symbol  Meaning                     Presentation      Examples
  22.   ------  -------                     ------------      -------
  23.    u       year                        year              2004; 04
  24.    y       year-of-era                 year              2004; 04
  25.    M/L     month-of-year               number/text       7; 07; Jul; July; J
  26.    d       day-of-month                number            10
  27.  
  28.  
  29. Symbol  Meaning                     Presentation      Examples
  30.   ------  -------                     ------------      -------
  31.    H       hour-of-day (0-23)          number            0
  32.    m       minute-of-hour              number            30
  33.    s       second-of-minute            number            55
  34.    S       fraction-of-second          fraction          978
  35.    n       nano-of-second              number            987654321
  36.  *
  37.  * */
  38.  
  39. @Data
  40. public class TestDateDto {
  41.         public static final String DATE_TIME_PATTERN = "dd-MM-yyyy HH:mm:ss";
  42.         public static final String DATE_PATTERN = "dd-MM-yyyy";
  43.         public static final String DATE_TIME_EXAMPLE = "12-05-2021 12:01:02";
  44.         public static final String DATE_EXAMPLE = "12-05-2021";
  45.        
  46.         @JsonFormat (pattern = DATE_PATTERN)
  47.         @Schema(example = DATE_EXAMPLE,  type = "string")
  48.         LocalDate localDate = LocalDate.now();
  49.        
  50.         @JsonFormat (pattern = DATE_TIME_PATTERN)
  51.         @Schema(example = DATE_TIME_EXAMPLE,  type = "string")
  52.         LocalDateTime localDateTime = LocalDateTime.now();
  53.        
  54.         LocalDate localDateNormal = LocalDate.now();
  55.        
  56.         public  TestDateDto() {
  57.                 LocalDate anotherSummerDay = LocalDate.of(2016, 8, 23);
  58.                 Locale.setDefault(Locale.ITALY);
  59.                
  60.         localDateNormal.format(DateTimeFormatter.ISO_DATE);
  61.         DateTimeFormatter
  62.         .ISO_OFFSET_DATE
  63.         .format(
  64.                         LocalDate.of(2018, 3, 9)
  65.                         .atStartOfDay(ZoneId.of("UTC-3"))
  66.        );
  67.        
  68.         }
  69.                
  70. }
  71.