是否可以获取具有特定格式的 java.util.Date 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19207477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:02:24  来源:igfitidea点击:

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

javadatesimpledateformat

提问by Sai Ye Yan Naing Aye

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

我想问一下java.util.Date的用法。这是我的示例类

         public class DateConverter {
           public static void main(String[] args) {
                  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
                  Date today = new Date();
                  String dateAsString_format = simpleDateFormat.format(today);
                  System.out.println("Formatted Date String (String datatype): " + dateAsString_format);
                  Date parsedDate = null;
                  try {
                      parsedDate = simpleDateFormat.parse(dateAsString_format);
                  } catch (ParseException e) {
                      e.printStackTrace();
                  }
                  System.out.println("Parse Date (Date datatype): " + parsedDate);
               }
            }

My output is

我的输出是

         Formatted Date String (String datatype): 06/10/2013
         Parse Date (Date datatype): Sun Oct 06 00:00:00 MMT 2013

But I would like to get the following output

但我想得到以下输出

         Formatted Date String (String datatype): 06/10/2013
         Parse Date (Date datatype): 06/10/2013

Is it possible to get Date object with specific format?

是否可以获取具有特定格式的 Date 对象?

采纳答案by Rohit Jain

Is it possible to get Date object with specific format?

是否可以获取具有特定格式的 Date 对象?

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

Date没有任何格式。它表示自纪元以来的毫秒数。您只能使用 获得格式化的字符串SimpleDateFormat,您已经这样做了。

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

打印Date调用覆盖的Date#toString()方法,该方法使用默认格式,在其中Date打印每个。

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

这是Date#toString()源的样子:

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"

所以使用的格式是 "EEE MMM dd HH:mm:ss zzz yyyy"

回答by Audrius Meskauskas

Yes, extend Dateadding the format field and override toStringmethod, something along the lines of

是的,扩展Date添加格式字段和覆盖toString方法,类似于

public class DateWithFormat extends Date {
   String format; // Assign as appropriate
   public String toString() {
     return new SimpleDateFormat(format).format(this));
   } 
}