0

I have a Date object in DTO object:

public class TopTerminalsDTO {

    private Date date;

    private int volume;

    private int count;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getVolume() {
        return volume;
    }

    public void setVolume(int volume) {
        this.volume = volume;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

When I get the response in Angular I get

count: 1
date: "2018-10-06T00:00:00.000+0000"
volume: 111

I want to get this date format YYYY-MM-DD HH:mm:ss in Angular.

What is the proper way to convert the Date into the DTO object? Is it better to use LocalDateTime?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    Possible duplicate of [Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects](https://stackoverflow.com/questions/52870774/convert-java-util-date-to-string-in-yyyy-mm-dd-format-without-creating-a-lot-of) – Sharon Ben Asher Mar 13 '19 at 10:08
  • Can you add the relevant part i mean where and how are you setting date in `TopTerminalsDTO `? – Sudhir Ojha Mar 13 '19 at 10:08
  • If you're using Spring Web or whatever, you need to make it clear in your question. – ttzn Mar 13 '19 at 10:17
  • side note: not use Date class instead of use new class LocalDate https://stackoverflow.com/questions/48688736/are-java-util-date-and-java-util-calendar-deprecated – Akash Shah Mar 13 '19 at 10:23

4 Answers4

2

It's better to use LocalDateTime object, but it will return it with a T between the date and hours. You should remove it like in the selected answer here LocalDate - How to remove character 'T' in LocalDate

Benjamin
  • 89
  • 5
1

User the Below Code.

Date myDate = new Date();
System.out.println(new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").format(myDate));
Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
1

LocalDate is the preferred way of many developers since it's been released in Java 8. You can format a LocalDate object the way you want by using the .format(DateTimeFormatter) method of LocalDate.

Like this example from: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

LocalDate date = LocalDate.now();
String text = date.format(formatter);
LocalDate parsedDate = LocalDate.parse(text, formatter);

Edit:

The LocalDate class doens't provide a time representation. Therefore if you like to also have time, use the LocalDateTime class. The .format() method of LocalDateTime can be used like the .format() method of LocalDate as shown above.

Moduo
  • 623
  • 6
  • 17
  • But I need hours and minutes. With LocalDate I get date: "2018-11-06" only. – Peter Penzov Mar 13 '19 at 10:35
  • 1
    I added additional ifnormation about `LocalDateTime`. You can use this to also have a time representation in your field. The formatter works the same as with `LocalDate`. – Moduo Mar 13 '19 at 10:57
1

U can use DateFormat to convert your desire date format.

TopTerminalsDTO tt = new TopTerminalsDTO();
tt.setDate(new Date());
String strDateFormat = "YYYY-MM-DD HH:mm:ss";
DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
String formattedDate= dateFormat.format(tt.getDate());
System.out.println(formattedDate);

As you are sending rest object to angular so u can use string field as date in DTO once covert it in desire date format.

Nitin Jangid
  • 164
  • 1
  • 3