修改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 代码?
- protected?String?resolve(TemplateContext?context)?...{??
- ????????//?return?DateFormat.getDateInstance().format(new?java.util.Date());??
- ????????//?Modified?by?yawolf@gmail.com??
- ????????final?SimpleDateFormat?df?=?new?SimpleDateFormat("yyyy/MM/dd");??
- ????????return?df.format(new?java.util.Date());??
- ????}??
?
?时间格式修改为:
java 代码?
- /**
- ?*?{@inheritDoc}?
- ?*/??
- protected?String?resolve(TemplateContext?context)?...{??
- ????//?return?DateFormat.getTimeInstance().format(new?java.util.Date());??
- ????//?Modified?by?yawolf@gmail.com??
- ????final?SimpleDateFormat?ldf?=?new?SimpleDateFormat("HH:mm:ss");??
- ????return?ldf.format(new?java.util.Date());??
- }??
?
2、将修改过的类编译,然后再打包成org.eclipse.text_3.3.0.v20070503-0800.jar文件,并放进Eclipse插件目录。
3、重启Eclipse系统,即可使用新的&{Date}及%{Time}格式。
以下是我的文件头注释模板:
java 代码?
- /**
- ?*?@(#)${file_name}?1.0.0?${date}?${time}?
- ?*?
- ?*?Copyright?${year}?Cepiao?Co.?Ltd.??All?rights?reserved.?
- ?*?CEPIAO?PROPRIETARY/CONFIDENTIAL.?Use?is?subject?to?license?terms.?
- ?*/??
?
类注释模板:
java 代码?
- /**??
- ?*?Class?${type_name}?
- ?*?
- ?*?@author??
- ?*?@version?$$Revision:1.0.0,?$$Date:?${date}?${time}?$$?
- ?*?${tags}?
- ?*/??
?
以下是新生成的Test类:
java 代码?
- /**?
- ?*?@(#)Test.java?1.0.0?2007/05/10?14:10:11?
- ?*?
- ?*?Copyright?2007?Cepiao?Co.?Ltd.??All?rights?reserved.?
- ?*?CEPIAO?PROPRIETARY/CONFIDENTIAL.?Use?is?subject?to?license?terms.?
- ?*/??
- package?com.cepiao.test;??
- ??
- /**?
- ?*?Class?Test?
- ?*?
- ?*?@author??
- ?*?@version?$Revision:1.0.0,?$Date:?2007/05/10?14:10:11?$?
- ?*/??
- public?class?Test?{??
- ??
- }??
?
发下是修改过的GlobalTemplateVariables类源代码:
java 代码?
- /*******************************************************************************?
- ?*?Copyright?(c)?2000,?2006?IBM?Corporation?and?others.?
- ?*?All?rights?reserved.?This?program?and?the?accompanying?materials?
- ?*?are?made?available?under?the?terms?of?the?Eclipse?Public?License?v1.0?
- ?*?which?accompanies?this?distribution,?and?is?available?at?
- ?*?http://www.eclipse.org/legal/epl-v10.html?
- ?*?
- ?*?Contributors:?
- ?*?????IBM?Corporation?-?initial?API?and?implementation?
- ?*?????Sebastian?Davids:?sdavids@gmx.de?-?see?bug?25376?
- ?*******************************************************************************/??
- package?org.eclipse.jface.text.templates;??
- ??
- import?java.text.SimpleDateFormat;??
- ??
- //import?com.ibm.icu.text.DateFormat;??
- import?com.ibm.icu.util.Calendar;??
- ??
- /**??
- ?*?Global?variables?which?are?available?in?any?context.?
- ?*?
?
? - ?*?Clients?may?instantiate?the?classes?contained?within?this?class.?
- ?*?
?
? - ?*??
- ?*?@since?3.0?
- ?*/??
- public?class?GlobalTemplateVariables?...{??
- ??
- ????/**??The?type?of?the?selection?variables.?*/??
- ????public?static?final?String?SELECTION?=?"selection";?//$NON-NLS-1$??
- ??
- ????/**??
- ?????*?The?cursor?variable?determines?the?cursor?placement?after?template?
- ?????*?edition.?
- ?????*/??
- ????public?static?class?Cursor?extends?SimpleTemplateVariableResolver?...{??
- ??
- ????????/**?Name?of?the?cursor?variable,?value=?{@value}?*/??
- ????????public?static?final?String?NAME?=?"cursor";?//$NON-NLS-1$??
- ??
- ????????/**??
- ?????????*?Creates?a?new?cursor?variable?
- ?????????*/??
- ????????public?Cursor()?...{??
- ????????????super(NAME,?TextTemplateMessages??
- ????????????????????.getString("GlobalVariables.variable.description.cursor"));?//$NON-NLS-1$??
- ????????????setEvaluationString("");?//$NON-NLS-1$??
- ????????}??
- ????}??
- ??
- ????/**??
- ?????*?The?word?selection?variable?determines?templates?that?work?on?a?full?
- ?????*?lines?selection.?
- ?????*/??
- ????public?static?class?WordSelection?extends?SimpleTemplateVariableResolver?...{??
- ??
- ????????/**??Name?of?the?word?selection?variable,?value=?{@value}?*/??
- ????????public?static?final?String?NAME?=?"word_selection";?//$NON-NLS-1$??
- ??
- ????????/**??
- ?????????*?Creates?a?new?word?selection?variable?
- ?????????*/??
- ????????public?WordSelection()?...{??
- ????????????super(??
- ????????????????????NAME,??
- ????????????????????TextTemplateMessages??
- ????????????????????????????.getString("GlobalVariables.variable.description.selectedWord"));?//$NON-NLS-1$??
- ????????}??
- ??
- ????????protected?String?resolve(TemplateContext?context)?...{??
- ????????????String?selection?=?context.getVariable(SELECTION);??
- ????????????if?(selection?==?null)??
- ????????????????return?"";?//$NON-NLS-1$??
- ????????????return?selection;??
- ????????}??
- ????}??
- ??
- ????/**??
- ?????*?The?line?selection?variable?determines?templates?that?work?on?selected?
- ?????*?lines.?
- ?????*/??
- ????public?static?class?LineSelection?extends?SimpleTemplateVariableResolver?...{??
- ??
- ????????/**??Name?of?the?line?selection?variable,?value=?{@value}?*/??
- ????????public?static?final?String?NAME?=?"line_selection";?//$NON-NLS-1$??
- ??
- ????????/**??
- ?????????*?Creates?a?new?line?selection?variable?
- ?????????*/??
- ????????public?LineSelection()?...{??
- ????????????super(??
- ????????????????????NAME,??
- ????????????????????TextTemplateMessages??
- ????????????????????????????.getString("GlobalVariables.variable.description.selectedLines"));?//$NON-NLS-1$??
- ????????}??
- ??
- ????????protected?String?resolve(TemplateContext?context)?...{??
- ????????????String?selection?=?context.getVariable(SELECTION);??
- ????????????if?(selection?==?null)??
- ????????????????return?"";?//$NON-NLS-1$??
- ????????????return?selection;??
- ????????}??
- ????}??
- ??
- ????/**??
- ?????*?The?dollar?variable?inserts?an?escaped?dollar?symbol.?
- ?????*/??
- ????public?static?class?Dollar?extends?SimpleTemplateVariableResolver?...{??
- ????????/**?
- ?????????*?Creates?a?new?dollar?variable?
- ?????????*/??
- ????????public?Dollar()?...{??
- ????????????super(??
- ????????????????????"dollar",?TextTemplateMessages.getString("GlobalVariables.variable.description.dollar"));?//$NON-NLS-1$?//$NON-NLS-2$??
- ????????????setEvaluationString("$");?//$NON-NLS-1$??
- ????????}??
- ????}??
- ??
- ????/**??
- ?????*?The?date?variable?evaluates?to?the?current?date.?
- ?????*/??
- ????public?static?class?Date?extends?SimpleTemplateVariableResolver?...{??
- ????????/**??
- ?????????*?Creates?a?new?date?variable?
- ?????????*/??
- ????????public?Date()?...{??
- ????????????super(??
- ????????????????????"date",?TextTemplateMessages.getString("GlobalVariables.variable.description.date"));?//$NON-NLS-1$?//$NON-NLS-2$??
- ????????}??
- ??
- ????????protected?String?resolve(TemplateContext?context)?...{??
- ????????????//?return?DateFormat.getDateInstance().format(new?java.util.Date());??
- ????????????//?Modified?by?yawolf@gmail.com??
- ????????????final?SimpleDateFormat?df?=?new?SimpleDateFormat("yyyy/MM/dd");??
- ????????????return?df.format(new?java.util.Date());??
- ????????}??
- ????}??
- ??
- ????/**??
- ?????*?The?year?variable?evaluates?to?the?current?year.?
- ?????*/??
- ????public?static?class?Year?extends?SimpleTemplateVariableResolver?...{??
- ????????/**?
- ?????????*?Creates?a?new?year?variable?
- ?????????*/??
- ????????public?Year()?...{??
- ????????????super(??
- ????????????????????"year",?TextTemplateMessages.getString("GlobalVariables.variable.description.year"));?//$NON-NLS-1$?//$NON-NLS-2$??
- ????????}??
- ??
- ????????protected?String?resolve(TemplateContext?context)?...{??
- ????????????return?Integer.toString(Calendar.getInstance().get(Calendar.YEAR));??
- ????????}??
- ????}??
- ??
- ????/**?
- ?????*?The?time?variable?evaluates?to?the?current?time.?
- ?????*/??
- ????public?static?class?Time?extends?SimpleTemplateVariableResolver?...{??
- ????????/**??
- ?????????*?Creates?a?new?time?variable?
- ?????????*/??
- ????????public?Time()?...{??
- ????????????super(??
- ????????????????????"time",?TextTemplateMessages.getString("GlobalVariables.variable.description.time"));?//$NON-NLS-1$?//$NON-NLS-2$??
- ????????}??
- ??
- ????????/**??
- ?????????*?{@inheritDoc}?
- ?????????*/??
- ????????protected?String?resolve(TemplateContext?context)?...{??
- ????????????//?return?DateFormat.getTimeInstance().format(new?java.util.Date());??
- ????????????//?Modified?by?yawolf@gmail.com??
- ????????????final?SimpleDateFormat?ldf?=?new?SimpleDateFormat("HH:mm:ss");??
- ????????????return?ldf.format(new?java.util.Date());??
- ????????}??
- ????}??
- ??
- ????/**??
- ?????*?The?user?variable?evaluates?to?the?current?user.?
- ?????*/??
- ????public?static?class?User?extends?SimpleTemplateVariableResolver?...{??
- ????????/**??
- ?????????*?Creates?a?new?user?name?variable?
- ?????????*/??
- ????????public?User()?...{??
- ????????????super(??
- ????????????????????"user",?TextTemplateMessages.getString("GlobalVariables.variable.description.user"));?//$NON-NLS-1$?//$NON-NLS-2$??
- ????????}??
- ??
- ????????/**??
- ?????????*?{@inheritDoc}?
- ?????????*/??
- ????????protected?String?resolve(TemplateContext?context)?...{??
- ????????????return?System.getProperty("user.name");?//$NON-NLS-1$??
- ????????}??
- ????}??
- }??
?