Best way to convert Java SQL Date from yyyy-MM-dd to dd MMMM yyyy format

后端 未结 4 1371
有刺的猬
有刺的猬 2020-11-27 23:25

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

4条回答
  •  醉酒成梦
    2020-11-27 23:52

    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.

提交回复
热议问题