zoukankan      html  css  js  c++  java
  • 后端处理前端传过来的日期的两种方式

    一、注解处理

    1、在实体中加入注解@JsonFormat:

    @JsonFormat(pattern="yyyy-MM-dd HH:mm")
    private Date measurementtime;


    2、@JsonFormat         @DateTimeFormat            @Temporal 日期注解区分

    @JsonFormat 此注解用于属性或者方法上(最好是属性上),可以方便的把Date类型直接转化为我们想要的模式
    @JsonFormat(pattern="yyyy-MM-dd HH:mm")
    private Date measurementtime;

    @DateTimeFormat 格式化日期使用@DatetimeFormat很简单,这里需要注意的是:使用时要引入一个类库joda-time-1.3.jar,否则会无法访问相应路径(400错误)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date installdate;

    @Temporal Hibernate注解
              如果在某类中有Date类型的属性,数据库中存储可能是’yyyy-MM-dd hh:MM:ss’要在查询时获得年月日,在该属性上标注@Temporal(TemporalType.DATE) 会得到形如’yyyy-MM-dd’ 格式的日期。
              如果在某类中有Date类型的属性,数据库中存储可能是’yyyy-MM-dd hh:MM:ss’要获得时分秒,在该属性上标注 @Temporal(TemporalType.TIME) 会得到形如’HH:MM:SS’ 格式的日期。
             如果在某类中有Date类型的属性,数据库中存储可能是’yyyy-MM-dd hh:MM:ss’要获得’是’yyyy-MM-dd hh:MM:ss’,在该属性上标注 @Temporal(TemporalType.TIMESTAMP) 会得到形如’HH:MM:SS’ 格式的日期


    @Temporal(TemporalType.TIMESTAMP)
    private Date acceptancedate;

    二、工具类处理

    import org.springframework.util.StringUtils;

    import java.text.SimpleDateFormat;
    import java.util.Date;

    /**
    * @ClassName: DateConverter
    * @Description:
    * @Version: v1.0.0
    * @Author: Fu Hao
    * @Date: 2019/11/24 0024 下午 9:02
    * Modification History:
    * Date Author Version Description
    * -------------------------------------------------------------
    * 2019/11/24 0024 Fu Hao v1.0.0 创建
    */
    public class DateConverter {
    private static final String dateFormat = "yyyy-MM-dd HH:mm:ss";
    private static final String shortDateFormat = "yyyy-MM-dd";
    private static final String timeStampFormat = "^\d+$";

    private static final String hDateFormat = "yyyyMMdd HH:mm:ss";
    private static final String hshortDateFormat = "yyyyMMdd";


    public static Date convert(String value) {

    if (StringUtils.isEmpty(value)) {
    return null;
    }

    value = value.trim();

    try {
    if (value.contains("-")) {
    SimpleDateFormat formatter;
    if (value.contains(":")) {
    formatter = new SimpleDateFormat(dateFormat);
    } else {
    formatter = new SimpleDateFormat(shortDateFormat);
    }
    return formatter.parse(value);
    } else if (value.matches(timeStampFormat)) {
    Long lDate = new Long(value);
    return new Date(lDate);
    } else if (value.contains("")) {

    SimpleDateFormat formatter;
    if (value.contains(":")) {
    formatter = new SimpleDateFormat(hDateFormat);
    } else {
    formatter = new SimpleDateFormat(hshortDateFormat);
    }

    return formatter.parse(value);
    }
    } catch (Exception e) {
    throw new RuntimeException(String.format("parser %s to Date fail", value));
    }
    throw new RuntimeException(String.format("parser %s to Date fail", value));
    }
    }


    技术交流请加扣扣:863967089
  • 相关阅读:
    百度地图API地理位置和坐标转换
    WebClient 通过get和post请求api
    C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse
    C#模拟POST提交表单(一)--WebClient
    百度外卖接口调试 C#版
    DWZ(JUI) 教程 跨域请求 iframeNavTab
    订餐系统之同步美团商家订单
    订餐系统之同步饿了么商家订单
    外卖订单爬虫(美团,饿了么,百度外卖)
    订餐系统之获取淘宝外卖订单
  • 原文地址:https://www.cnblogs.com/xiaoshenke/p/11924369.html
Copyright © 2011-2022 走看看