• JAVA 响应时,按照指定的日期格式返回


    说明:

    数据响应时,指定日期格式为“yyyy/MM/dd”,需要注意的是,接收的数据是String类型的,返回的数据是Date类型的。

    第一种方式:在属性上添加注解(无效)

    @JsonFormat(pattern="yyyy/MM/dd")
    private Date createData;

    第二种方式:直接通过format的方式(有效)

        public String getCreateDate() {
            if (createDate == null) {
                return "";
            }
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
            return simpleDateFormat.format(createDate);
        }
    
        public void setAgreeDate(Date createDate) {
            this.createDate= createDate;
        }

    第三:附上Date和String转换的方法

    package datetest;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class DataTest {
    
     
        public static void main(String[] args) {
          
            //将Date类型转成String类
           
            Date now=new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String data=dateFormat.format(now);
           
            //将String类型转成Date类型
            String  mydate="2014/2/2 17:02:12";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            try {
                Date date=sdf.parse(mydate);
           
            } catch (ParseException ex) {
                Logger.getLogger(DataTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    以上是我的分享,如果大家有更好的方法,可以给我留言哦。

  • 相关阅读:
    《JavaScript语言精粹》学习心得
    Linq笔记
    关于缓存
    JS-替换全部全部字符串
    相同数据用分号分割
    单例模式
    es6- ArrayBuffer
    vue常用属性解释。
    装饰者模式
    中介者模式
  • 原文地址:https://www.cnblogs.com/kaile/p/10869680.html
Copyright © 2020-2023  润新知