Is it possible to get java.util.Date object with specific format?

前端 未结 2 2002
太阳男子
太阳男子 2021-01-15 14:37

I would like to ask about the usage of java.util.Date. Here is my sample class

         public class DateConverter {
           public static void main(Strin         


        
2条回答
  •  [愿得一人]
    2021-01-15 15:07

    Is it possible to get Date object with specific format?

    No. Date doesn't have any format. It represents the number of milliseconds since epoch. You only get the formatted string using SimpleDateFormat, which you already did.

    Printing Date invokes the overridden Date#toString() method, which uses a default format, in which every Date is printed.

    Here's how Date#toString() source looks like:

    public String toString() {
        // "EEE MMM dd HH:mm:ss zzz yyyy";
        BaseCalendar.Date date = normalize();
        StringBuilder sb = new StringBuilder(28);
        int index = date.getDayOfWeek();
        if (index == gcal.SUNDAY) {
            index = 8;
        }
        ....  // some more code
    }
    

    So the format used is "EEE MMM dd HH:mm:ss zzz yyyy"

提交回复
热议问题