当前位置: 首页 > 知识库问答 >
问题:

如何将java.sql.Date格式化为在JSON中接收的JavaScript日期?[关闭]

叶俊郎
2023-03-14

我有一个由Spring管理的简单实体。我想使用enddatestartdate字段,我在JavaScript中以JSON格式接收它们

收到JSON-

{
    "id": 151,
    "company": {
        "id": 1,
        "name": "companyName5"
    },
    "category": "Automotive",
    "title": "Automotive",
    "description": "Automotive",
    "startDate": "2021-06-30",
    "endDate": "2022-09-30",
    "amount": 50,
    "price": 50,
    "image": "imgPath"
}

Java实体-

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.*;
import javax.persistence.*;
import java.sql.Date;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
@Table(name = "coupon")
public class Coupon {

    // FIELDS
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;  

    @ManyToOne()
    @ToString.Exclude
    @JsonIdentityReference(alwaysAsId = true)
    @JsonIgnoreProperties({"email","password","coupons"})
    private Company company; 

    @Enumerated(EnumType.STRING)
    private Category category; 

    private String title;
    private String description; 
    private Date startDate; 
    private Date endDate; 
    private int amount; 
    private double price; 
    private String image; 
}

JavaScript(TypeScript)中的优惠券模型

class Coupon {
    public id: number = 0; 
    public comapnyId:number = 0;
    public category:string = "";
    public title:string = "";
    public description: string = "";
    public startDate: string;
    public endDate: string;
    public amount:number = 0;
    public price:number = 0;
    public image:string = "";
}

共有1个答案

韩佐
2023-03-14
    // parse JSON to JS object, get startDate and endDate fields via object destructuring   
    const {startDate, endDATE} = JSON.parse(`{
        "id": 151,
        "company": {
            "id": 1,
            "name": "companyName5"
        },
        "category": "Automotive",
        "title": "Automotive",
        "description": "Automotive",
        "startDate": "2021-06-30",
        "endDate": "2022-09-30",
        "amount": 50,
        "price": 50,
        "image": "imgPath"
    }`);
    
    // instantiate Date js objects with startDate and endDate provided to Date constructor
    const jsObjStartDate = new Date(startDate);
    const jsObjEndDate = new Date(endDate);
 类似资料:
  • 问题内容: 我正在使用需要json日期格式的API。 我需要转换我的JavaScript日期 1970年1月17日星期六格林尼治标准时间+0100(浪漫标准时间) 以json日期格式 /日期(1405699200)/ 问题答案: 列各项可以吗?

  • 问题内容: 在JavaScript中,如何格式化日期对象以打印为? 问题答案: 对于自定义分隔的日期格式,你必须从DateTimeFormat对象(属于ECMAScript Internationalization API的一部分)中提取日期(或时间)组件,然后手动使用所需的分隔符创建字符串。 为此,你可以使用DateTimeFormat#formatToParts: 你还可以使用来一对一地提取各

  • 问题内容: 我正在使用jQuery在Ajax上进行首次尝试。我正在将数据获取到页面上,但是为Date数据类型返回的JSON数据遇到了一些麻烦。基本上,我得到的字符串看起来像这样: 从完全不熟悉JSON的人- 如何将其格式化为短日期格式?是否应该在jQuery代码中的某个地方处理?我尝试使用该插件没有成功。 仅供参考:这是我结合以下答案使用的解决方案: 该解决方案从回调方法中获取了我的对象,并使用日

  • 我如何从moment.js获得ISO 8601?

  • 问题内容: 我有这样的日期格式。我必须格式化以使用JavaScript或jQuery。任何人都可以帮助我做同样的事情。 问题答案: 尝试这个; 请记住,JavaScript月的索引是0,而天的索引是1。

  • 这是一个很难回答的问题: 如何在没有外部库的情况下解析< code >“YYYYmmdd”格式的日期?如果输入字符串不是这种格式,我希望得到< code>invalid Date(或者undefined,如果这样更简单的话)。