Intellij 13日期的代码模板

时间:2014-10-30 12:54:13

标签: java date templates intellij-idea

目前我正在使用Java 7,我有一个如下所示的实用程序功能:

public static Date generateSafeDate(Date date){
   return new Date(date.getTime());
}

有没有办法让intellij每当我使用Date返回类型创建一个getter时生成这个语句:

原始代码:

public Date getDate(){
    return this.date;
}

进入这个:

public Date getDate(){
    return generateSafeDate(this.date);
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

转到设置 - >文件和代码模板 - >您看到的新方法主体

#if ( $RETURN_TYPE != "void" )return $DEFAULT_RETURN_VALUE;#end

替换为

#if ( $RETURN_TYPE != "Date" )
     return new Date($DEFAULT_RETURN_VALUE.getTime());
#else
#if ( $RETURN_TYPE != "void" )return $DEFAULT_RETURN_VALUE;#end 
#end