Eclipse 修改注釋的 date time 日期時間格式,即${date}變數格式

來源:互聯網
上載者:User

標籤:eclipse   注釋   格式   date   

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包的版本問題)

/******************************************************************************* * 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: [email protected] - see bug 25376 *******************************************************************************/package org.eclipse.jface.text.templates;import com.ibm.icu.text.DateFormat;import com.ibm.icu.util.Calendar;/** * Global variables which are available in any context. * <p> * Clients may instantiate the classes contained within this class. * </p> * * @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());}}/** * 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());}}/** * 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$}}}

自行拿去修改就行了。改一下Date Time Year就行了,例如,我修改的結果如下:

/******************************************************************************* * 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: [email protected] - see bug 25376 *******************************************************************************/package org.eclipse.jface.text.templates;import java.text.SimpleDateFormat;import java.util.Calendar;/** * Global variables which are available in any context. * <p> * Clients may instantiate the classes contained within this class. * </p> * * @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());    final SimpleDateFormat df = new SimpleDateFormat(TextTemplateMessages.getString("GlobalVariables.variable.format.date"));             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));    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) {    final SimpleDateFormat df = new SimpleDateFormat(TextTemplateMessages.getString("GlobalVariables.variable.format.time"));            return df.format(new java.util.Date()); //return DateFormat.getTimeInstance().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$}}}

我改成了使用

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

並且從properties檔案中讀取format格式,可以借鑒。


我提供編譯好的class檔案供大家下載(下載下面的圖片,把jpg尾碼 改成rar尾碼,然後開啟),替換到原檔案即可。










Eclipse 修改注釋的 date time 日期時間格式,即${date}變數格式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.