更改日期格式Java [英] Change Date format Java
问题描述
我尝试使用简单的日期格式化程序更改日期的格式,但我想将其保留为日期。现在,这是我在做什么:
I'm trying to change the format of a date using a simple date formatter, but I want to keep it as a date. At the moment, this is what I am doing:
public static Date getDate()
{
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
df.format(c.getTime());
return df.format(c.getTime());
}
目前以字符串的形式返回日期,返回日期?我不确定这个。
This currently returns the date as a string, but how could I get it to return as a date? I'm unsure about this.
推荐答案
我只是猜测,但如果你想定义另一个字符串格式的java .util.Date,而不是总是必须通过日期格式化运行,那么你可以继承Date和覆盖toString方法。
I'm just guessing, but if you want to define another string format of a java.util.Date and not always have to run that through a dateformatter, then you can subclass Date and override the toString-method.
这样:
public class MyDate extends Date {
public MyDate() {
super();
}
@Override
public String toString() {
return new SimpleDateFormat("dd/MM/yyyy").format(this);
}
public static void main(String[] args) {
MyDate d1 = new MyDate();
System.out.println(d1);
}
}
我仍然不相信自己这是一个好主意,但它是可能的...
I have still not convinced myself that this is a good idea, but it's possible ...
这篇关于更改日期格式Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!