I'm creating an email using String Template but when I print out a date, it prints out the full date (eg. Wed Apr 28 10:51:37 BST 2010). I'd like to print it out in the format dd/mm/yyyy but don't know how to format this in the .st file.
我正在使用字符串模板创建一个电子邮件,但是当我打印出一个日期时,它会打印出完整的日期(例如,2010年4月28日星期三,星期五,2010年5月5日,星期五)。我想以dd / mm / yyyy格式打印出来,但不知道如何在.st文件中格式化它。
I can't modify the date individually (using java's simpleDateFormatter) because I iterate over a collection of objects with dates.
我不能单独修改日期(使用java的simpleDateFormatter)因为我迭代了带有日期的对象集合。
Is there a way to format the date in the .st email template?
有没有办法格式化.st电子邮件模板中的日期?
4 个解决方案
#1
7
Use additional renderers like this:
使用这样的其他渲染器:
internal class AdvancedDateTimeRenderer : IAttributeRenderer
{
public string ToString(object o)
{
return ToString(o, null);
}
public string ToString(object o, string formatName)
{
if (o == null)
return null;
if (string.IsNullOrEmpty(formatName))
return o.ToString();
DateTime dt = Convert.ToDateTime(o);
return string.Format("{0:" + formatName + "}", dt);
}
}
and then add this to your StringTemplate such as:
然后将其添加到StringTemplate中,例如:
var stg = new StringTemplateGroup("Templates", path);
stg.RegisterAttributeRenderer(typeof(DateTime), new AdvancedDateTimeRenderer());
then in st file:
然后在st文件中:
$YourDateVariable; format="dd/mm/yyyy"$
it should work
它应该工作
#2
2
Here is a basic Java example, see StringTemplate documentation on Object Rendering for more information.
这是一个基本的Java示例,有关详细信息,请参阅有关对象渲染的StringTemplate文档。
StringTemplate st = new StringTemplate("now = $now$");
st.setAttribute("now", new Date());
st.registerRenderer(Date.class, new AttributeRenderer(){
public String toString(Object date) {
SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
return f.format((Date) date);
}
});
st.toString();
#3
1
StringTemplate 4 includes a DateRenderer class.
StringTemplate 4包含一个DateRenderer类。
My example below is a modified version of the NumberRenderer on the documentation on Renderers in Java
下面的示例是Java中Renderers文档中NumberRenderer的修改版本
String template =
"foo(right_now) ::= << <right_now; format=\"full\"> >>\n";
STGroup g = new STGroupString(template);
g.registerRenderer(Date.class, new DateRenderer());
ST st = group.getInstanceOf("foo");
st.add("right_now", new Date());
String result = st.render();
The provided options for format map as such:
为格式映射提供的选项如下:
- "short" => DateFormat.SHORT (default)
- "medium" => DateFormat.MEDIUM
- "long" => DateFormat.LONG
- "full" => DateFormat.FULL
“short”=> DateFormat.SHORT(默认)
“medium”=> DateFormat.MEDIUM
“long”=> DateFormat.LONG
“full”=> DateFormat.FULL
Or, you can use a custom format like so:
或者,您可以使用如下自定义格式:
foo(right_now) ::= << <right_now; format="MM/dd/yyyy"> >>
You can see these options and other details on the DateRenderer Java source here
您可以在此处查看DateRenderer Java源代码的这些选项和其他详细信息
#4
0
one very important fact while setting date format is to use "MM" instead of "mm" for month. "mm" is meant to be used for minutes. Using "mm" instead of "MM" very generally introduces bugs difficult to find.
设置日期格式时,一个非常重要的事实是使用“MM”而不是“mm”表示月份。 “mm”意味着使用几分钟。使用“mm”而不是“MM”通常会引入难以找到的错误。
#1
7
Use additional renderers like this:
使用这样的其他渲染器:
internal class AdvancedDateTimeRenderer : IAttributeRenderer
{
public string ToString(object o)
{
return ToString(o, null);
}
public string ToString(object o, string formatName)
{
if (o == null)
return null;
if (string.IsNullOrEmpty(formatName))
return o.ToString();
DateTime dt = Convert.ToDateTime(o);
return string.Format("{0:" + formatName + "}", dt);
}
}
and then add this to your StringTemplate such as:
然后将其添加到StringTemplate中,例如:
var stg = new StringTemplateGroup("Templates", path);
stg.RegisterAttributeRenderer(typeof(DateTime), new AdvancedDateTimeRenderer());
then in st file:
然后在st文件中:
$YourDateVariable; format="dd/mm/yyyy"$
it should work
它应该工作
#2
2
Here is a basic Java example, see StringTemplate documentation on Object Rendering for more information.
这是一个基本的Java示例,有关详细信息,请参阅有关对象渲染的StringTemplate文档。
StringTemplate st = new StringTemplate("now = $now$");
st.setAttribute("now", new Date());
st.registerRenderer(Date.class, new AttributeRenderer(){
public String toString(Object date) {
SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
return f.format((Date) date);
}
});
st.toString();
#3
1
StringTemplate 4 includes a DateRenderer class.
StringTemplate 4包含一个DateRenderer类。
My example below is a modified version of the NumberRenderer on the documentation on Renderers in Java
下面的示例是Java中Renderers文档中NumberRenderer的修改版本
String template =
"foo(right_now) ::= << <right_now; format=\"full\"> >>\n";
STGroup g = new STGroupString(template);
g.registerRenderer(Date.class, new DateRenderer());
ST st = group.getInstanceOf("foo");
st.add("right_now", new Date());
String result = st.render();
The provided options for format map as such:
为格式映射提供的选项如下:
- "short" => DateFormat.SHORT (default)
- "medium" => DateFormat.MEDIUM
- "long" => DateFormat.LONG
- "full" => DateFormat.FULL
“short”=> DateFormat.SHORT(默认)
“medium”=> DateFormat.MEDIUM
“long”=> DateFormat.LONG
“full”=> DateFormat.FULL
Or, you can use a custom format like so:
或者,您可以使用如下自定义格式:
foo(right_now) ::= << <right_now; format="MM/dd/yyyy"> >>
You can see these options and other details on the DateRenderer Java source here
您可以在此处查看DateRenderer Java源代码的这些选项和其他详细信息
#4
0
one very important fact while setting date format is to use "MM" instead of "mm" for month. "mm" is meant to be used for minutes. Using "mm" instead of "MM" very generally introduces bugs difficult to find.
设置日期格式时,一个非常重要的事实是使用“MM”而不是“mm”表示月份。 “mm”意味着使用几分钟。使用“mm”而不是“MM”通常会引入难以找到的错误。