简体   繁体   English

用Java转换日期格式

[英]Convert Date format in Java

I have a Date object in DTO object: 我在DTO对象中有一个Date对象:

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 当我在Angular中得到响应时

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. 我想在Angular中获取此日期格式YYYY-MM-DD HH:mm:ss

What is the proper way to convert the Date into the DTO object? 将Date转换为DTO对象的正确方法是什么? Is it better to use LocalDateTime? 使用LocalDateTime更好吗?

It's better to use LocalDateTime object, but it will return it with a T between the date and hours. 最好使用LocalDateTime对象,但它将在日期和小时之间返回T。 You should remove it like in the selected answer here LocalDate - How to remove character 'T' in LocalDate 您应该像在此处所选答案中一样将其删除LocalDate-如何LocalDate中删除字符“ T”

User the Below Code. 使用以下代码。

Date myDate = new Date();
System.out.println(new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").format(myDate));

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 . LocalDate是许多开发商的首选方法,因为它是用Java 8发布可以格式化LocalDate使用对象,你想要的方式.format(DateTimeFormatter)的方法LocalDate

Like this example from: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html 像来自以下示例的示例: 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. LocalDate类不提供时间表示。 Therefore if you like to also have time, use the LocalDateTime class. 因此,如果您还希望有时间,请使用LocalDateTime类。 The .format() method of LocalDateTime can be used like the .format() method of LocalDate as shown above. 所述.format()的方法LocalDateTime可以像使用.format()的方法LocalDate如上所示。

U can use DateFormat to convert your desire date format. U可以使用DateFormat转换所需的日期格式。

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. 当您将剩余对象发送到有角度的对象时,一旦您将其以所需的日期格式进行隐蔽,您就可以将字符串字段用作DTO中的日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM