Eclipse 修改注释的 date time 日期时间格式,即${date}变量格式

http://blog.csdn.net/zollty/article/details/46459621

Eclipse 修改注释的 date time 日期时间格式,即${date}变量格式


找到eclipse安装目录下面的plugins目录,搜索 org.eclipse.text ,找到一个jar包,
例如我找到的jar包为:org.eclipse.text_3.5.300.v20130515-1451.jar

然后打开它,找到这个类: org.eclipse.jface.text.templates.GlobalTemplateVariables

我们重写这个类就行了。(可反编译,也可以找到源码,源码下载地址为:http://Git.eclipse.org/c/platform/eclipse.platform.text.git,下载zip包) PS:如果嫌下载源码包麻烦,我这里贴出这个文件的源码,可以直接用(注:这个类很简单,无多少依赖,所有版本通用,无需担心jar包的版本问题)

  1. /******************************************************************************* 
  2.  * Copyright (c) 2000, 2006 IBM Corporation and others. 
  3.  * All rights reserved. This program and the accompanying materials 
  4.  * are made available under the terms of the Eclipse Public License v1.0 
  5.  * which accompanies this distribution, and is available at 
  6.  * http://www.eclipse.org/legal/epl-v10.html 
  7.  * 
  8.  * Contributors: 
  9.  *     IBM Corporation - initial API and implementation 
  10.  *     Sebastian Davids: sdavids@gmx.de - see bug 25376 
  11.  *******************************************************************************/  
  12. package org.eclipse.jface.text.templates;  
  13.   
  14. import com.ibm.icu.text.DateFormat;  
  15. import com.ibm.icu.util.Calendar;  
  16.   
  17. /** 
  18.  * Global variables which are available in any context. 
  19.  * <p> 
  20.  * Clients may instantiate the classes contained within this class. 
  21.  * </p> 
  22.  * 
  23.  * @since 3.0 
  24.  */  
  25. public class GlobalTemplateVariables {  
  26.   
  27.     /** The type of the selection variables. */  
  28.     public static final String SELECTION= "selection"//$NON-NLS-1$  
  29.   
  30.     /** 
  31.      * The cursor variable determines the cursor placement after template edition. 
  32.      */  
  33.     public static class Cursor extends SimpleTemplateVariableResolver {  
  34.   
  35.         /** Name of the cursor variable, value= {@value} */  
  36.         public static final String NAME= "cursor"//$NON-NLS-1$  
  37.   
  38.         /** 
  39.          * Creates a new cursor variable 
  40.          */  
  41.         public Cursor() {  
  42.             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.cursor")); //$NON-NLS-1$  
  43.             setEvaluationString(""); //$NON-NLS-1$  
  44.         }  
  45.     }  
  46.   
  47.     /** 
  48.      * The word selection variable determines templates that work on a full 
  49.      * lines selection. 
  50.      */  
  51.     public static class WordSelection extends SimpleTemplateVariableResolver {  
  52.   
  53.         /** Name of the word selection variable, value= {@value} */  
  54.         public static final String NAME= "word_selection"//$NON-NLS-1$  
  55.   
  56.         /** 
  57.          * Creates a new word selection variable 
  58.          */  
  59.         public WordSelection() {  
  60.             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$  
  61.         }  
  62.         protected String resolve(TemplateContext context) {  
  63.             String selection= context.getVariable(SELECTION);  
  64.             if (selection == null)  
  65.                 return ""//$NON-NLS-1$  
  66.             return selection;  
  67.         }  
  68.     }  
  69.   
  70.     /** 
  71.      * The line selection variable determines templates that work on selected 
  72.      * lines. 
  73.      */  
  74.     public static class LineSelection extends SimpleTemplateVariableResolver {  
  75.   
  76.         /** Name of the line selection variable, value= {@value} */  
  77.         public static final String NAME= "line_selection"//$NON-NLS-1$  
  78.   
  79.         /** 
  80.          * Creates a new line selection variable 
  81.          */  
  82.         public LineSelection() {  
  83.             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedLines")); //$NON-NLS-1$  
  84.         }  
  85.         protected String resolve(TemplateContext context) {  
  86.             String selection= context.getVariable(SELECTION);  
  87.             if (selection == null)  
  88.                 return ""//$NON-NLS-1$  
  89.             return selection;  
  90.         }  
  91.     }  
  92.   
  93.     /** 
  94.      * The dollar variable inserts an escaped dollar symbol. 
  95.      */  
  96.     public static class Dollar extends SimpleTemplateVariableResolver {  
  97.         /** 
  98.          * Creates a new dollar variable 
  99.          */  
  100.         public Dollar() {  
  101.             super("dollar", TextTemplateMessages.getString("GlobalVariables.variable.description.dollar")); //$NON-NLS-1$ //$NON-NLS-2$  
  102.             setEvaluationString("$"); //$NON-NLS-1$  
  103.         }  
  104.     }  
  105.   
  106.     /** 
  107.      * The date variable evaluates to the current date. 
  108.      */  
  109.     public static class Date extends SimpleTemplateVariableResolver {  
  110.         /** 
  111.          * Creates a new date variable 
  112.          */  
  113.         public Date() {  
  114.             super("date", TextTemplateMessages.getString("GlobalVariables.variable.description.date")); //$NON-NLS-1$ //$NON-NLS-2$  
  115.         }  
  116.         protected String resolve(TemplateContext context) {  
  117.             return DateFormat.getDateInstance().format(new java.util.Date());  
  118.         }  
  119.     }  
  120.   
  121.     /** 
  122.      * The year variable evaluates to the current year. 
  123.      */  
  124.     public static class Year extends SimpleTemplateVariableResolver {  
  125.         /** 
  126.          * Creates a new year variable 
  127.          */  
  128.         public Year() {  
  129.             super("year", TextTemplateMessages.getString("GlobalVariables.variable.description.year")); //$NON-NLS-1$ //$NON-NLS-2$  
  130.         }  
  131.         protected String resolve(TemplateContext context) {  
  132.             return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));  
  133.         }  
  134.     }  
  135.   
  136.     /** 
  137.      * The time variable evaluates to the current time. 
  138.      */  
  139.     public static class Time extends SimpleTemplateVariableResolver {  
  140.         /** 
  141.          * Creates a new time variable 
  142.          */  
  143.         public Time() {  
  144.             super("time", TextTemplateMessages.getString("GlobalVariables.variable.description.time")); //$NON-NLS-1$ //$NON-NLS-2$  
  145.         }  
  146.   
  147.         /** 
  148.          * {@inheritDoc} 
  149.          */  
  150.         protected String resolve(TemplateContext context) {  
  151.             return DateFormat.getTimeInstance().format(new java.util.Date());  
  152.         }  
  153.     }  
  154.   
  155.     /** 
  156.      * The user variable evaluates to the current user. 
  157.      */  
  158.     public static class User extends SimpleTemplateVariableResolver {  
  159.         /** 
  160.          * Creates a new user name variable 
  161.          */  
  162.         public User() {  
  163.             super("user", TextTemplateMessages.getString("GlobalVariables.variable.description.user")); //$NON-NLS-1$ //$NON-NLS-2$  
  164.         }  
  165.   
  166.         /** 
  167.          * {@inheritDoc} 
  168.          */  
  169.         protected String resolve(TemplateContext context) {  
  170.             return System.getProperty("user.name"); //$NON-NLS-1$  
  171.         }  
  172.     }  
  173. }  

自行拿去修改就行了。改一下Date Time Year就行了,例如,我修改的结果如下:
  1. /******************************************************************************* 
  2.  * Copyright (c) 2000, 2006 IBM Corporation and others. 
  3.  * All rights reserved. This program and the accompanying materials 
  4.  * are made available under the terms of the Eclipse Public License v1.0 
  5.  * which accompanies this distribution, and is available at 
  6.  * http://www.eclipse.org/legal/epl-v10.html 
  7.  * 
  8.  * Contributors: 
  9.  *     IBM Corporation - initial API and implementation 
  10.  *     Sebastian Davids: sdavids@gmx.de - see bug 25376 
  11.  *******************************************************************************/  
  12. package org.eclipse.jface.text.templates;  
  13.   
  14. import java.text.SimpleDateFormat;  
  15. import java.util.Calendar;  
  16.   
  17. /** 
  18.  * Global variables which are available in any context. 
  19.  * <p> 
  20.  * Clients may instantiate the classes contained within this class. 
  21.  * </p> 
  22.  * 
  23.  * @since 3.0 
  24.  */  
  25. public class GlobalTemplateVariables {  
  26.   
  27.     /** The type of the selection variables. */  
  28.     public static final String SELECTION= "selection"//$NON-NLS-1$  
  29.   
  30.     /** 
  31.      * The cursor variable determines the cursor placement after template edition. 
  32.      */  
  33.     public static class Cursor extends SimpleTemplateVariableResolver {  
  34.   
  35.         /** Name of the cursor variable, value= {@value} */  
  36.         public static final String NAME= "cursor"//$NON-NLS-1$  
  37.   
  38.         /** 
  39.          * Creates a new cursor variable 
  40.          */  
  41.         public Cursor() {  
  42.             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.cursor")); //$NON-NLS-1$  
  43.             setEvaluationString(""); //$NON-NLS-1$  
  44.         }  
  45.     }  
  46.   
  47.     /** 
  48.      * The word selection variable determines templates that work on a full 
  49.      * lines selection. 
  50.      */  
  51.     public static class WordSelection extends SimpleTemplateVariableResolver {  
  52.   
  53.         /** Name of the word selection variable, value= {@value} */  
  54.         public static final String NAME= "word_selection"//$NON-NLS-1$  
  55.   
  56.         /** 
  57.          * Creates a new word selection variable 
  58.          */  
  59.         public WordSelection() {  
  60.             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$  
  61.         }  
  62.         protected String resolve(TemplateContext context) {  
  63.             String selection= context.getVariable(SELECTION);  
  64.             if (selection == null)  
  65.                 return ""//$NON-NLS-1$  
  66.             return selection;  
  67.         }  
  68.     }  
  69.   
  70.     /** 
  71.      * The line selection variable determines templates that work on selected 
  72.      * lines. 
  73.      */  
  74.     public static class LineSelection extends SimpleTemplateVariableResolver {  
  75.   
  76.         /** Name of the line selection variable, value= {@value} */  
  77.         public static final String NAME= "line_selection"//$NON-NLS-1$  
  78.   
  79.         /** 
  80.          * Creates a new line selection variable 
  81.          */  
  82.         public LineSelection() {  
  83.             super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedLines")); //$NON-NLS-1$  
  84.         }  
  85.         protected String resolve(TemplateContext context) {  
  86.             String selection= context.getVariable(SELECTION);  
  87.             if (selection == null)  
  88.                 return ""//$NON-NLS-1$  
  89.             return selection;  
  90.         }  
  91.     }  
  92.   
  93.     /** 
  94.      * The dollar variable inserts an escaped dollar symbol. 
  95.      */  
  96.     public static class Dollar extends SimpleTemplateVariableResolver {  
  97.         /** 
  98.          * Creates a new dollar variable 
  99.          */  
  100.         public Dollar() {  
  101.             super("dollar", TextTemplateMessages.getString("GlobalVariables.variable.description.dollar")); //$NON-NLS-1$ //$NON-NLS-2$  
  102.             setEvaluationString("$"); //$NON-NLS-1$  
  103.         }  
  104.     }  
  105.   
  106.     /** 
  107.      * The date variable evaluates to the current date. 
  108.      */  
  109.     public static class Date extends SimpleTemplateVariableResolver {  
  110.         /** 
  111.          * Creates a new date variable 
  112.          */  
  113.         public Date() {  
  114.             super("date", TextTemplateMessages.getString("GlobalVariables.variable.description.date")); //$NON-NLS-1$ //$NON-NLS-2$  
  115.         }  
  116.         protected String resolve(TemplateContext context) {  
  117. //          return DateFormat.getDateInstance().format(new java.util.Date());  
  118.             final SimpleDateFormat df = new SimpleDateFormat(TextTemplateMessages.getString("GlobalVariables.variable.format.date"));   
  119.             return df.format(new java.util.Date());   
  120.         }  
  121.     }  
  122.   
  123.     /** 
  124.      * The year variable evaluates to the current year. 
  125.      */  
  126.     public static class Year extends SimpleTemplateVariableResolver {  
  127.         /** 
  128.          * Creates a new year variable 
  129.          */  
  130.         public Year() {  
  131.             super("year", TextTemplateMessages.getString("GlobalVariables.variable.description.year")); //$NON-NLS-1$ //$NON-NLS-2$  
  132.         }  
  133.         protected String resolve(TemplateContext context) {  
  134. //          return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));  
  135.             return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));    
  136.         }  
  137.     }  
  138.   
  139.     /** 
  140.      * The time variable evaluates to the current time. 
  141.      */  
  142.     public static class Time extends SimpleTemplateVariableResolver {  
  143.         /** 
  144.          * Creates a new time variable 
  145.          */  
  146.         public Time() {  
  147.             super("time", TextTemplateMessages.getString("GlobalVariables.variable.description.time")); //$NON-NLS-1$ //$NON-NLS-2$  
  148.         }  
  149.   
  150.         /** 
  151.          * {@inheritDoc} 
  152.          */  
  153.         protected String resolve(TemplateContext context) {  
  154.             final SimpleDateFormat df = new SimpleDateFormat(TextTemplateMessages.getString("GlobalVariables.variable.format.time"));  
  155.             return df.format(new java.util.Date());   
  156.             //return DateFormat.getTimeInstance().format(new java.util.Date());  
  157.         }  
  158.     }  
  159.   
  160.     /** 
  161.      * The user variable evaluates to the current user. 
  162.      */  
  163.     public static class User extends SimpleTemplateVariableResolver {  
  164.         /** 
  165.          * Creates a new user name variable 
  166.          */  
  167.         public User() {  
  168.             super("user", TextTemplateMessages.getString("GlobalVariables.variable.description.user")); //$NON-NLS-1$ //$NON-NLS-2$  
  169.         }  
  170.   
  171.         /** 
  172.          * {@inheritDoc} 
  173.          */  
  174.         protected String resolve(TemplateContext context) {  
  175.             return System.getProperty("user.name"); //$NON-NLS-1$  
  176.         }  
  177.     }  
  178. }  

我改成了使用

import Java.text.SimpleDateFormat;
import java.util.Calendar;

并且从properties文件中读取format格式,可以借鉴。


我提供编译好的class文件供大家下载(下载下面的图片,把jpg后缀 改成rar后缀,然后打开),替换到原文件即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值