`
yawolf
  • 浏览: 13382 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

修改Eclipse的Date变量格式

阅读更多

    自从Eclipse升级到3.2版本以后,其代码模板的当前日期变量{$Date}的格式就不再符合国人习惯。在3.2版本中,日期格式为“2007-5-10 上午06:58:10”格式;在3.3版本中,日期格式则为“2007 五月 10 17:20:02”格式。我还是习惯采用“yyyy/MM/dd HH:mm:ss”格式,但无论怎么修改Windows系统的区域设置,Eclipse的Date格式还是没有变化。

Eclipse 的Date变量在GlobalTemplateVariables类中定义,如果要修改日期格式,则需要修改GlobalTemplateVariables类。这个类在Eclipse插件目录org.eclipse.text_3.3.0.v20070503-0800.jar(3.3.0 M7版本)文件的org.eclipse.jface.text.templates包中,我的办法是:

1、在eclipse的源代码中修改org.eclipse.text项目的GlobalTemplateVariables类。

日期格式修改为:

java 代码
 
  1. protected String resolve(TemplateContext context) ...{  
  2.         // return DateFormat.getDateInstance().format(new java.util.Date());  
  3.         // Modified by yawolf@gmail.com  
  4.         final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");  
  5.         return df.format(new java.util.Date());  
  6.     }  

 

 时间格式修改为:

java 代码
 
  1. /**
  2.  * {@inheritDoc} 
  3.  */  
  4. protected String resolve(TemplateContext context) ...{  
  5.     // return DateFormat.getTimeInstance().format(new java.util.Date());  
  6.     // Modified by yawolf@gmail.com  
  7.     final SimpleDateFormat ldf = new SimpleDateFormat("HH:mm:ss");  
  8.     return ldf.format(new java.util.Date());  
  9. }  

 

2、将修改过的类编译,然后再打包成org.eclipse.text_3.3.0.v20070503-0800.jar文件,并放进Eclipse插件目录。

3、重启Eclipse系统,即可使用新的&{Date}及%{Time}格式。

以下是我的文件头注释模板:

java 代码
 
  1. /**
  2.  * @(#)${file_name} 1.0.0 ${date} ${time} 
  3.  * 
  4.  * Copyright ${year} Cepiao Co. Ltd.  All rights reserved. 
  5.  * CEPIAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
  6.  */  

 

类注释模板:

java 代码
 
  1. /**  
  2.  * Class ${type_name} 
  3.  * 
  4.  * @author  
  5.  * @version $$Revision:1.0.0, $$Date: ${date} ${time} $$ 
  6.  * ${tags} 
  7.  */  

 

以下是新生成的Test类:

java 代码
 
  1. /** 
  2.  * @(#)Test.java 1.0.0 2007/05/10 14:10:11 
  3.  * 
  4.  * Copyright 2007 Cepiao Co. Ltd.  All rights reserved. 
  5.  * CEPIAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
  6.  */  
  7. package com.cepiao.test;  
  8.   
  9. /** 
  10.  * Class Test 
  11.  * 
  12.  * @author  
  13.  * @version $Revision:1.0.0, $Date: 2007/05/10 14:10:11 $ 
  14.  */  
  15. public class Test {  
  16.   
  17. }  

 

发下是修改过的GlobalTemplateVariables类源代码:

java 代码
 
  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.   
  16. //import com.ibm.icu.text.DateFormat;  
  17. import com.ibm.icu.util.Calendar;  
  18.   
  19. /**  
  20.  * Global variables which are available in any context. 
  21.  * 

     

     
  22.  * Clients may instantiate the classes contained within this class. 
  23.  * 

     

     
  24.  *  
  25.  * @since 3.0 
  26.  */  
  27. public class GlobalTemplateVariables ...{  
  28.   
  29.     /**  The type of the selection variables. */  
  30.     public static final String SELECTION = "selection"//$NON-NLS-1$  
  31.   
  32.     /**  
  33.      * The cursor variable determines the cursor placement after template 
  34.      * edition. 
  35.      */  
  36.     public static class Cursor extends SimpleTemplateVariableResolver ...{  
  37.   
  38.         /** Name of the cursor variable, value= {@value} */  
  39.         public static final String NAME = "cursor"//$NON-NLS-1$  
  40.   
  41.         /**  
  42.          * Creates a new cursor variable 
  43.          */  
  44.         public Cursor() ...{  
  45.             super(NAME, TextTemplateMessages  
  46.                     .getString("GlobalVariables.variable.description.cursor")); //$NON-NLS-1$  
  47.             setEvaluationString(""); //$NON-NLS-1$  
  48.         }  
  49.     }  
  50.   
  51.     /**  
  52.      * The word selection variable determines templates that work on a full 
  53.      * lines selection. 
  54.      */  
  55.     public static class WordSelection extends SimpleTemplateVariableResolver ...{  
  56.   
  57.         /**  Name of the word selection variable, value= {@value} */  
  58.         public static final String NAME = "word_selection"//$NON-NLS-1$  
  59.   
  60.         /**  
  61.          * Creates a new word selection variable 
  62.          */  
  63.         public WordSelection() ...{  
  64.             super(  
  65.                     NAME,  
  66.                     TextTemplateMessages  
  67.                             .getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$  
  68.         }  
  69.   
  70.         protected String resolve(TemplateContext context) ...{  
  71.             String selection = context.getVariable(SELECTION);  
  72.             if (selection == null)  
  73.                 return ""//$NON-NLS-1$  
  74.             return selection;  
  75.         }  
  76.     }  
  77.   
  78.     /**  
  79.      * The line selection variable determines templates that work on selected 
  80.      * lines. 
  81.      */  
  82.     public static class LineSelection extends SimpleTemplateVariableResolver ...{  
  83.   
  84.         /**  Name of the line selection variable, value= {@value} */  
  85.         public static final String NAME = "line_selection"//$NON-NLS-1$  
  86.   
  87.         /**  
  88.          * Creates a new line selection variable 
  89.          */  
  90.         public LineSelection() ...{  
  91.             super(  
  92.                     NAME,  
  93.                     TextTemplateMessages  
  94.                             .getString("GlobalVariables.variable.description.selectedLines")); //$NON-NLS-1$  
  95.         }  
  96.   
  97.         protected String resolve(TemplateContext context) ...{  
  98.             String selection = context.getVariable(SELECTION);  
  99.             if (selection == null)  
  100.                 return ""//$NON-NLS-1$  
  101.             return selection;  
  102.         }  
  103.     }  
  104.   
  105.     /**  
  106.      * The dollar variable inserts an escaped dollar symbol. 
  107.      */  
  108.     public static class Dollar extends SimpleTemplateVariableResolver ...{  
  109.         /** 
  110.          * Creates a new dollar variable 
  111.          */  
  112.         public Dollar() ...{  
  113.             super(  
  114.                     "dollar", TextTemplateMessages.getString("GlobalVariables.variable.description.dollar")); //$NON-NLS-1$ //$NON-NLS-2$  
  115.             setEvaluationString("$"); //$NON-NLS-1$  
  116.         }  
  117.     }  
  118.   
  119.     /**  
  120.      * The date variable evaluates to the current date. 
  121.      */  
  122.     public static class Date extends SimpleTemplateVariableResolver ...{  
  123.         /**  
  124.          * Creates a new date variable 
  125.          */  
  126.         public Date() ...{  
  127.             super(  
  128.                     "date", TextTemplateMessages.getString("GlobalVariables.variable.description.date")); //$NON-NLS-1$ //$NON-NLS-2$  
  129.         }  
  130.   
  131.         protected String resolve(TemplateContext context) ...{  
  132.             // return DateFormat.getDateInstance().format(new java.util.Date());  
  133.             // Modified by yawolf@gmail.com  
  134.             final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");  
  135.             return df.format(new java.util.Date());  
  136.         }  
  137.     }  
  138.   
  139.     /**  
  140.      * The year variable evaluates to the current year. 
  141.      */  
  142.     public static class Year extends SimpleTemplateVariableResolver ...{  
  143.         /** 
  144.          * Creates a new year variable 
  145.          */  
  146.         public Year() ...{  
  147.             super(  
  148.                     "year", TextTemplateMessages.getString("GlobalVariables.variable.description.year")); //$NON-NLS-1$ //$NON-NLS-2$  
  149.         }  
  150.   
  151.         protected String resolve(TemplateContext context) ...{  
  152.             return Integer.toString(Calendar.getInstance().get(Calendar.YEAR));  
  153.         }  
  154.     }  
  155.   
  156.     /** 
  157.      * The time variable evaluates to the current time. 
  158.      */  
  159.     public static class Time extends SimpleTemplateVariableResolver ...{  
  160.         /**  
  161.          * Creates a new time variable 
  162.          */  
  163.         public Time() ...{  
  164.             super(  
  165.                     "time", TextTemplateMessages.getString("GlobalVariables.variable.description.time")); //$NON-NLS-1$ //$NON-NLS-2$  
  166.         }  
  167.   
  168.         /**  
  169.          * {@inheritDoc} 
  170.          */  
  171.         protected String resolve(TemplateContext context) ...{  
  172.             // return DateFormat.getTimeInstance().format(new java.util.Date());  
  173.             // Modified by yawolf@gmail.com  
  174.             final SimpleDateFormat ldf = new SimpleDateFormat("HH:mm:ss");  
  175.             return ldf.format(new java.util.Date());  
  176.         }  
  177.     }  
  178.   
  179.     /**  
  180.      * The user variable evaluates to the current user. 
  181.      */  
  182.     public static class User extends SimpleTemplateVariableResolver ...{  
  183.         /**  
  184.          * Creates a new user name variable 
  185.          */  
  186.         public User() ...{  
  187.             super(  
  188.                     "user", TextTemplateMessages.getString("GlobalVariables.variable.description.user")); //$NON-NLS-1$ //$NON-NLS-2$  
  189.         }  
  190.   
  191.         /**  
  192.          * {@inheritDoc} 
  193.          */  
  194.         protected String resolve(TemplateContext context) ...{  
  195.             return System.getProperty("user.name"); //$NON-NLS-1$  
  196.         }  
  197.     }  
  198. }  

 

分享到:
评论

相关推荐

    Eclipse注释模板变量补丁

    Eclipse的代码注释模板很丰富,如:user、year、date、time等等,通过在eclipse.ini文件中-Duser.name=xxxx进行配置,但是Eclipse的Preferences界面至今都不支持定制自己的注释模板变量,要新增一个注释变量的话,就...

    修改Eclipse注释里的${Date}变量格式

    附件是一个重新编译好的jar包文件,因为不让传.jar文件,所以我把后缀改成了.zip,下载后直接将后缀名改为.jar即可。 博文链接:https://ttitfly.iteye.com/blog/154044

    eclipse java注释模板

    <templates><template autoinsert="true" context="fieldcomment_context" deleted="false" description="字段的注释" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.fieldcomment" name="fieldcomment...

    eclipse 开发c/c++

    C 和 C++ 语言都是世界上最流行且使用最普遍的编程语言, 因此 Eclipse 平台(Eclipse Platform)提供对 C/C++ 开发的支持一点都不足为奇。 因为 Eclipse 平台只是用于开发者工具的一个框架,它不直接支持 C/C++;它...

    tomcat环境变量配置

    2,JAVA_HOME指明JDK安装路径,此路径下包括lib,bin,jre等文件夹,tomcat,eclipse等的运行都需要依靠此变量。 3,PATH使得系统可以在任何路径下识别java命令。 4,CLASSPATH为java加载类(class or lib)路径,...

    JDT生成代码实例

    String[] imports = {"java.util.Date", "java.util.Random"}; for(String imp : imports){ //创建一个新包声名 ImportDeclaration importDeclaration = ast.newImportDeclaration(); //添加包说明 ...

    简易定时执行程序和提醒功能小软件

    1. 解压,然后将文件导入Eclipse; 2. 运行该程序前,要先安装mysql; 3. 安装完mysql后,下载mysql-connector-java-5.1.22; 4. 将mysql-connector-java-5.1.22放入JDK中,方法见本人博客博文《java数据库编程的...

    freemarker语法完整版

    encoding="GBK" 编码格式 parse=true 是否作为ftl语法解析,默认是true,false就是以文本方式引入.注意在ftl文件里布尔值都是直接赋值 的如parse=true,而不是parse="true" 用例 /common/copyright.ftl 包含内容 ...

    21天学通Java-由浅入深

    108 6.6 对象引用的使用 110 6.6.1 调用不存在的对象或成员变量 110 6.6.2 调用对象为null值的引用 111 6.6.3 对象引用间的比较 113 6.7 this 113 6.8 要活用JDK已有的类 114 6.8.1 Date类 114 6.8.2 Integer类 116 ...

    精通正则表达式基于.NET ASP PHP JSP JavaScript

    ASPNETValidator/RangeDate.aspx 日期范围验证 ASPNETValidator/RangeInt.aspx 整数范围验证 ASPNETValidator/RangeLetter.aspx 字母范围验证 ASPNETValidator/REDate.aspx 日期格式验证 ...

    php网络开发完全手册

    4.2.4 格式化本地时间日期的函数date 62 4.2.5 获得本地化时间戳的函数mktime 64 4.2.6 输出控制函数flush 65 4.2.7 变量检测函数isset与变量释放 4.2.7 函数unset 66 4.2.8 随机函数rand与srand 66 4.3 关于引用的...

    Java开发技术大全 电子版

    3.3.3实例成员变量和静态成员变量114 3.4方法的定义和实现116 3.4.1方法的声明117 3.4.2创建方法体与return语句117 3.4.3局部变量和成员变量的区别119 3.4.4方法的访问权限121 3.5方法的调用121 3.5.1方法...

    java内部学习笔记.docx

    1.6 Eclipse/Myeclipse程序结构 6 Java语言基础 7 2.1基础语言要素 7 2.2八种基本数据类型 7 2.3常量和变量 8 2.4运算符与表达式 8 2.5编程风格 9 2.6流程控制语句 10 2.7数组 11 2.8字符串 11 2.9方法三要素 12 ...

    疯狂JAVA讲义

    学生提问:老师,我想学习Java编程,到底是学习Eclipse好呢,还是学习JBuilder好呢? 21 1.9 本章小结 22 本章练习 22 第2章 理解面向对象 23 2.1 面向对象 24 2.1.1 结构化程序设计简介 24 2.1.2 程序的三种...

    JAVA基础课程讲义

    Date时间类(java.util.Date) 124 DateFormat类和SimpleDateFormat类 125 Calendar日历类 126 可视化日历的编写 128 Math类 131 File类 132 File类的基本用法 132 树状结构展现文件结构 133 枚举 133 上机作业 135 第...

    Ext Js权威指南(.zip.001

    8.1.3 格式化输出数据:ext.string、ext.number、ext.date和ext.util.format / 389 8.1.4 超级模板:ext.xtemplate(包括ext.xtemplateparser和ext.xtemplatecompiler) / 393 8.1.5 模板的方法 / 396 8.2 组件...

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................

Global site tag (gtag.js) - Google Analytics