1、入参和出参格式化
入参可以在对应的入参类里或者参数列表上加@DateTimeFormat注解来标注格式,从数据库查询数据转换成PO类时可以在对应的字段上使用@JsonFormat注解,示例如下
@Data
@ApiModel("医嘱参数类")
public class AppOrderDTO implements Serializable {
private static final long serialVersionUID = 713282991594955019L;
@ApiModelProperty("开始时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date startTime;
@ApiModelProperty("结束时间")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date endTime;
}
package com.wghis.cpoe.interfaces.order.viewobject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wghis.cpoe.infrastructure.constant.StandardCodeConstant;
import com.wghis.translate.annotation.TranslateField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
/**
* <p>
* 给项目补录界面查询申请单列表使用
* </p>
*
* @author zhy
* @since 2021-12-07 14:52:24
*/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AppOrderVO implements Serializable {
private static final long serialVersionUID = 653300411253997656L;
@ApiModelProperty("申请时间")
@JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
private Date applyTime;
}
2、获取某一天的开始和结束时间
通过jdk8的日期操作可以很简单的进行转换
/**
* 获取某天最大时间
* */
public static Date getEndOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* 获取某天最小时间
* */
public static Date getStartOfDay(Date date) {
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());
LocalDateTime startOfDay = localDateTime.with(LocalTime.MIN);
return Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
}
在oracle数据库sql层面可以利用TRUNC函数
<if test="o.shiftBeginTime != null">
and CPOE_DOC_DATE >= TRUNC(#{o.shiftBeginTime})
</if>
<if test="o.shiftEndTime != null">
and CPOE_DOC_DATE <= TRUNC(#{o.shiftEndTime})+1-1/86400
</if>
mysql数据库sql层面可以利用DATE_ADD函数和DATE_FORMAT函数
<if test="o.shiftBeginTime != null">
and CPOE_DOC_DATE >= str_to_date(DATE_FORMAT(o.shiftBeginTime,'%Y-%m-%d'),'%Y-%m-%d %H:%i:%s')
</if>
<if test="o.shiftEndTime != null">
and CPOE_DOC_DATE <= DATE_ADD(DATE_ADD(str_to_date(DATE_FORMAT(o.shiftEndTime,'%Y-%m-%d'),'%Y-%m-%d %H:%i:%s'),INTERVAL 1 DAY),INTERVAL -1 SECOND)
</if>