读书人

批改Eclipse的Date变量格式

发布时间: 2012-11-06 14:07:00 作者: rapoo

修改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开发

热点推荐