简体   繁体   中英

Intellij 13 code template for Dates

At the moment I am using Java 7 and I have a utility function that looks like this:

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

is there a way to make intellij generate this statement everytime I make a getter with a Date return type:

original code:

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

into this:

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

You can try this.

Go to Settings -> File and Code Templates -> New Method Body you see

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

replace it with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM