使用Format类格式化日期(否则为if) [英] Formatting Dates using a Format class (or otherwise if )
问题描述
我想使用名为 DateFormat
的标准类,它有子类 SimpleDateFormat
要编写一个名为convert的方法,它返回一个dd.mm.yy形式的字符串:当传递具有特定日期的GregorianCalendar时
i want to use standard class called DateFormat
which has subclass SimpleDateFormat
TO write a method called convert which returns a String in the form dd.mm.yy: when passed a GregorianCalendar with a specific date
public String convert (Calendar gc) { ... }
例如,当myGC是 GregorianCalendar
表示2006年12月25日的变量, String s = convert(myGC);
应将s设置为字符串25.12.06。
For example, when myGC is a GregorianCalendar
variable representing the 25th of December 2006, String s = convert(myGC);
should set s to the string "25.12.06".
我在编写转换方法时遇到了麻烦
and i'm having trouble to write a convert method on this
推荐答案
为什么不呢只需在你的SimpleDateFormat中使用像这样的模式dd.MM.yy
?
Why not just use a pattern like this one "dd.MM.yy"
in your SimpleDateFormat ?
DateFormat dateFormatter = new SimpleDateFormat("dd.MM.yy");
String myDate = dateFormatter.format(cal.getTime());
这篇关于使用Format类格式化日期(否则为if)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!