将Java SQL Date从yyyy-MM-dd转换为dd MMMM yyyy格式的最佳方式 [英] Best way to convert Java SQL Date from yyyy-MM-dd to dd MMMM yyyy format
问题描述
我可以将日期转换为字符串,直接将Java日期从格式yyyy-MM-dd转换为dd MMMM yyyy格式吗?然后操纵它,但我宁愿将其作为Java SQL Date。当我需要这样做时,数据已经从MySQL数据库读取,所以我不能在那里进行更改。
谢谢
Morgan先生。
对象如 java.sql.Date
和 java.util.Date
(其中 java.sql.Date
是一个子类)没有自己的格式。您使用 java.text.DateFormat
对象以特定格式显示这些对象,它是 DateFormat
(不是
例如:
日期日期= ...; //任何你得到这个
DateFormat df = new SimpleDateFormat(dd MMMM yyyy);
String text = df.format(date);
System.out.println(text);
注意:打印 Date
不使用 DateFormat
对象,如下所示:
Date date =。 ..;
System.out.println(date);
然后它将使用一些默认格式进行格式化。然而,该默认格式不是您可以更改的 Date
对象的属性。
Is there a straightforward way of converting a Java SQL Date from format yyyy-MM-dd to dd MMMM yyyy format?
I could convert the date to a string and then manipulate it but I'd rather leave it as a Java SQL Date. at the time I need to do this, the data has already been read from the MySQL database so I cant do the change there.
Thanks
Mr Morgan.
Object such as java.sql.Date
and java.util.Date
(of which java.sql.Date
is a subclass) don't have a format of themselves. You use a java.text.DateFormat
object to display these objects in a specific format, and it's the DateFormat
(not the Date
itself) that determines the format.
For example:
Date date = ...; // wherever you get this
DateFormat df = new SimpleDateFormat("dd MMMM yyyy");
String text = df.format(date);
System.out.println(text);
Note: When you print a Date
object without using a DateFormat
object, like this:
Date date = ...;
System.out.println(date);
then it will be formatted using some default format. That default format is however not a property of the Date
object that you can change.
这篇关于将Java SQL Date从yyyy-MM-dd转换为dd MMMM yyyy格式的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!