-1

I'm using LocalDateTime.now() and OffSetDateTime.now() to retrieve the date and time but it's not the desired format. Does anyone know how to format this format? "2022-05-20T11:22:10"

I'm using MapStruct to do request and response conversions:

@Mapper
public interface ParesMapper {

    ParesMapper INSTANCE = Mappers.getMapper(ParesMapper.class);

    @Mapping(target = "data", source = "data")
    DataParesResponse toResponse(DataPares domain);

    @Mapping(target = "data", source = "data")
    List<DataParesResponse> toResponseList(List<DataPares> domain);

    DataPares toDomain(DataParesRequest dto);

    default OffsetDateTime map(LocalDateTime value) {
        return OffsetDateTime.now();
    }
    default LocalDateTime map(OffsetDateTime value) {
        return LocalDateTime.now();
    }
}

I look forward to helping you find a solution.

ner
  • 173
  • 1
  • 1
  • 9
  • You probably don’t want `LocalDateTime.now()` since anyone would be free to interpret the result in their own time zone, which would be wrong. `OffsetDateTime.now()` is fine since it includes offset. BTW, which is your desired format? And which purpose do you want it for (presentation to user, interchange with another system, persistence, …)? – Ole V.V. Aug 03 '22 at 04:37
  • Yes correct. I would like to use it to present to the user. – ner Aug 03 '22 at 11:54
  • It's not clear what your question is, partly due to the ambiguous phrasing, but mostly from lack of information. Is "2022-05-20T11:22:10" an example of the desired output, or the input you need to convert? Specify what information you have as input, and what format you require as output. For example, do you want to use the current time as input? Or do you have a string representing some stored date-time? Or some type of temporal object? What format do you want to display to the user? What time zone should be used for display? – erickson Aug 03 '22 at 16:31

1 Answers1

1

You can use DateTimeFormatter method

    import java.time.format.DateTimeFormatter;
    import java.time.LocalDateTime;
    public class DateTimeFormatter {
        public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        System.out.println(dtf.format(now));
    }
}