功能代码块

2018-10-17  本文已影响0人  掘金_蒋老湿

获取当前时间的前或后n个小时

      /**
     * Description: 获取当前时间的前或后n个小时
     * @param date  日期
     * @param hour  时间
     * @return  Date
     */
    public static Date getDayHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hour);
        return calendar.getTime();
    }

忽略对象字段转json及忽略json field对应赋值给object field

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 忽略带有此注解object field转json
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreJson {
}

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 忽略带有此注解json field对应赋值给object field
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreObject {
}
class FXContact {
  @IgnoreJson
  @Column(name = "s_fxxk_id")
  var ks_owner: String = _
  @IgnoreObject
  var owner: Array[String] = _
  @IgnoreJson
  @SerializedName("owner")
  var fx_owner: String = _
}
  private val gson = new GsonBuilder().setExclusionStrategies(new GsonObjectExclusionStrategy).create()
/**
  * Gson转json时忽略对某域的序列化转换
  */
class GsonJsonExclusionStrategy extends ExclusionStrategy {
  override def shouldSkipField(fieldAttributes: FieldAttributes): Boolean = fieldAttributes.getAnnotation(classOf[IgnoreJson]) != null

  override def shouldSkipClass(clazz: Class[_]): Boolean = clazz.getAnnotation(classOf[IgnoreJson]) != null
}
class GsonObjectExclusionStrategy extends ExclusionStrategy {
  override def shouldSkipField(fieldAttributes: FieldAttributes): Boolean = fieldAttributes.getAnnotation(classOf[IgnoreObject]) != null

  override def shouldSkipClass(clazz: Class[_]): Boolean = clazz.getAnnotation(classOf[IgnoreObject]) != null
}
上一篇 下一篇

猜你喜欢

热点阅读