【问题标题】:Adding Timestamp to a hashmap Post. How to?将时间戳添加到 hashmap Post。如何?
【发布时间】:2012-12-21 03:07:45
【问题描述】:

所以,我正在尝试将 TIMESTAMP 添加到 hashmap 帖子中。 (规则 -publish_date 和 expiration_date) 我正在使用在 jSon 中编码的 api 将数据发布到远程服务器。

这是 POST 格式的示例

    content={
    "external_id": 1000,
    "template_type_id": 103,
    "channel_id": 226,
    "title": "Título do Conteúdo",
    "text": "Descrição do Conteúdo",
    "rules": {
        "publish_date": "2012-07-20T11:18:00-03:00",
        "expiration_date": "2012-08-25T11:18:00-03:00",
        "notify_publish": true,
        "notify_expiration": false,
        "highlighted": true
    },
    "interactions": {
        "allow_comment": true,
        "auto_download": false
    }
}

首先我创建了一个名为 ContentDTO 的类并添加了它的代码

package br.com.xxxx.xxxx;

public class ContentDTO {

    public ContentDTO(String external_id, Integer template_type_id, String channel_id, String title, String text, RulesDTO rules, InteractionsDTO interactions) {
        super();
        this.external_id = external_id;
        this.template_type_id = template_type_id;
        this.channel_id = channel_id;
        this.title = title;
        this.text = text;
        this.rules = rules;
        this.interactions = interactions;
    }

    public ContentDTO() {
        super();
    }

    private String external_id; 

    private Integer template_type_id;

    private String channel_id;

    private String title;

    private String text;

    private RulesDTO rules;

    private InteractionsDTO interactions;

    public String getExternal_id() {
        return external_id;
    }

    public void setExternal_id(String external_id) {
        this.external_id = external_id;
    }

    public Integer getTemplate_type_id() {
        return template_type_id;
    }

    public void setTemplate_type_id(Integer template_type_id) {
        this.template_type_id = template_type_id;
    }

    public String getChannel_id() {
        return channel_id;
    }

    public void setChannel_id(String channel_id) {
        this.channel_id = channel_id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public RulesDTO getRules() {
        return rules;
    }

    public void setRules(RulesDTO rules) {
        this.rules = rules;
    }

    public InteractionsDTO getInteractions() {
        return interactions;
    }

    public void setInteractions(InteractionsDTO interactions) {
        this.interactions = interactions;
    }

}

然后,我创建了 InteractionsDTO 和 RulesDTO。这是 RulesDTO 代码。

package br.com.xxxx.xxxx;

import java.security.Timestamp;

public class RulesDTO {

    public RulesDTO(Timestamp publish_date, Timestamp expiration_date,
            Boolean notify_publish, Boolean notify_expiration,
            Boolean highlihted) {
        super();
        this.publish_date = publish_date;
        this.expiration_date = expiration_date;
        this.notify_publish = notify_publish;
        this.notify_expiration = notify_expiration;
        this.highlihted = highlihted;
    }

    public RulesDTO() {
        super();

    }

    public Timestamp publish_date;

    public Timestamp expiration_date;

    public Boolean notify_publish;

    public Boolean notify_expiration;

    public Boolean highlihted;

    public Timestamp getPublish_date() {
        return publish_date;
    }

    public void setPublish_date(Timestamp publish_date) {
        this.publish_date = publish_date;
    }

    public Timestamp getExpiration_date() {
        return expiration_date;
    }

    public void setExpiration_date(Timestamp expiration_date) {
        this.expiration_date = expiration_date;
    }

    public Boolean getNotify_publish() {
        return notify_publish;
    }

    public void setNotify_publish(Boolean notify_publish) {
        this.notify_publish = notify_publish;
    }

    public Boolean getNotify_expiration() {
        return notify_expiration;
    }

    public void setNotify_expiration(Boolean notify_expiration) {
        this.notify_expiration = notify_expiration;
    }

    public Boolean getHighlihted() {
        return highlihted;
    }

    public void setHighlihted(Boolean highlihted) {
        this.highlihted = highlihted;
    }



}

然后,哈希图。

HashMap<String, ContentDTO> cnt = new HashMap<String, ContentDTO>();

    ContentDTO contentDTO = new ContentDTO();
    contentDTO.setExternal_id("CNT1");
    contentDTO.setTemplate_type_id(103);
    contentDTO.setChannel_id("CHN1");
    contentDTO.setTitle("Conteudo1");
    contentDTO.setText("Conteudo teste 1");
    RulesDTO rules = new RulesDTO();
    rules.setPublish_date("2012-012-28T11:18:00-03:00");
    rules.setExpiration_date("2013-08-25T11:18:00-03:00");
    rules.setNotify_publish(true);
    rules.setNotify_expiration(false);
    rules.setHighlihted(true);

    contentDTO.setRules(rules);

    InteractionsDTO interactions = new InteractionsDTO();
    interactions.setAllow_comment(true);
    interactions.setAuto_downloa(false);

    contentDTO.setInteractions(interactions);


    cnt.put("content",contentDTO);

但我在 publish_date 和 expiration_date 中收到错误。 (RulesDTO中的setPublish_date(timestamp)方法不适用于参数(字符串))

我该怎么办?

谢谢转发!

【问题讨论】:

    标签: java spring post hashmap timestamp


    【解决方案1】:

    你有

    public void setPublish_date(Timestamp publish_date) {
    

    rules.setPublish_date("2012-012-28T11:18:00-03:00");
    

    Timestamp 不是 String,您必须将 "2012-012...." 字符串转换为 Timestamp 对象,SimpleDateFormat.parse(...)Timestamp.valueOf(...) 可以帮助您 - 但请注意,您可能希望将 december 编码为“012”,而是使用“12”...

    干杯,

    【讨论】:

      【解决方案2】:

      setPublish_date(Timestamp) 需要 Timestamp 而不是 String 作为参数。你可以用Timestamp.valueOf转换你的字符串

      rules.setPublish_date(Timestamp.valueOf("2012-01-28 11:18:00"));
      

      【讨论】:

      • 谢谢!感谢您的快速帮助!
      • @SergioMorganti 如果这解决了您的问题,请考虑accepting 的答案之一。谢谢。
      猜你喜欢
      • 2019-11-19
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2010-10-26
      • 2014-03-18
      相关资源
      最近更新 更多