【问题标题】:unexpected output while converting a string to date in java在java中将字符串转换为日期时出现意外输出
【发布时间】:2012-03-03 02:38:29
【问题描述】:

我有一个字符串"12/9/2010 4:39:38 PM",我必须将其转换为日期对象。我正在使用以下代码来做到这一点:

String str = "12/9/2010 4:39:38 PM";

DateFormat formatter ;

Date date ;

formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");

date =(Date)formatter.parse(str);             

System.out.println("date printed"+date);      

但是,当我打印输出时,我看到了

2010 年 12 月 9 日星期四 04:39:38 IST

如何完全按照我在字符串中声明的方式获取日期,即

2010 年 12 月 9 日下午 4:39:38

作为输出?请帮忙

【问题讨论】:

  • 为什么需要再次转换成相同的格式?
  • System.out.println("date printed"+date); 正在引用 Date.toString() 方法。这就是打印日期的格式看起来“奇怪”的原因,也就是 toString() 使用的格式。如果要以默认格式以外的格式显示日期,则必须使用日期格式化程序(如下面的所有当前答案所示)。
  • @thinksteep:我在数组列表中有这些日期的字符串格式“12/9/2010 4:39:38 PM”。我必须找到最新的。所以我需要对它们进行排序。对于排序,我无法执行字符串排序。所以我将它转换为日期对象。然后使用自定义排序执行排序,然后当我尝试打印最新日期时,其格式将更改为“Thu Dec 09 04:39:38 IST 2010”。这就是我面临的问题。现在解决了。

标签: java string date format


【解决方案1】:

您假设 Date 值本身会记住格式 - 但事实并非如此。 Date.toString 会做它想做的事——因为Date 只代表一个瞬间。

如果您想格式化 Date,请再次使用您的格式化程序:

System.out.println(formatter.format(date));

但是,这不会必然返回与字符串中完全相同的值,因为可能有多个值以相同的方式解析。例如,由于您只使用了“H:m:s”,我希望“4:5:6”的解析方式与“04:05:06”相同。

【讨论】:

    【解决方案2】:

    您可以使用 Formatter 类完全指定日期输出的格式

    简答

    String str = "12/9/2010 4:39:38 PM";
    Formatter formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
    Date date =(Date)formatter.parse(str);
    Formatter formatterOutput = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
    String s = formatterOutput.format(date);
    

    其他例子

    Format formatter;
    
    // The year
    formatter = new SimpleDateFormat("yy");    // 02
    formatter = new SimpleDateFormat("yyyy");  // 2002
    
    // The month
    formatter = new SimpleDateFormat("M");     // 1
    formatter = new SimpleDateFormat("MM");    // 01
    formatter = new SimpleDateFormat("MMM");   // Jan
    formatter = new SimpleDateFormat("MMMM");  // January
    
    // The day
    formatter = new SimpleDateFormat("d");     // 9
    formatter = new SimpleDateFormat("dd");    // 09
    
    // The day in week
    formatter = new SimpleDateFormat("E");     // Wed
    formatter = new SimpleDateFormat("EEEE");  // Wednesday
    
    // Get today's date
    Date date = new Date();
    
    // Some examples
    formatter = new SimpleDateFormat("MM/dd/yy");
    String s = formatter.format(date);
    // 01/09/02
    
    formatter = new SimpleDateFormat("dd-MMM-yy");
    s = formatter.format(date);
    // 29-Jan-02
    
    // Examples with date and time; see also
    // Formatting the Time Using a Custom Format
    formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
    s = formatter.format(date);
    // 2002.01.29.08.36.33
    
    formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
    s = formatter.format(date);
    // Tue, 09 Jan 2002 22:14:02 -0500
    

    来自:http://www.exampledepot.com/egs/java.text/formatdate.html

    【讨论】:

    • 感谢您的详细解释。真的很有帮助
    【解决方案3】:

    使用相同的格式化程序:

    System.out.println("date printed "+ formatter.format(date));
    

    【讨论】:

      【解决方案4】:

      使用 SimpleDateFormat 将其转换回字符串?

      【讨论】:

        【解决方案5】:
        formatter = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
        String temp =formatter.format(date ); 
        

        【讨论】:

        • 在问题中,String被解析为Date,反之则不然。
        【解决方案6】:

        Java.util.Date 没有内在格式的概念 - 您需要使用 format(java.util.Date d) 方法来查看 Date 对象的格式化 String 表示。

        String str = "12/9/2010 4:39:38 PM";
        
        DateFormat formatter = new SimpleDateFormat("M/dd/yyyy H:m:s a");
        Date date =(Date)formatter.parse(str);
        
        System.out.println("date printed"+formatter.format(date));
        

        【讨论】:

          【解决方案7】:

          不确定您要完成什么。但是你必须调用 SimpleDateFormat.format() 才能得到你所期望的。直接打印日期只会得到 Date 的 toString() 实现

          【讨论】:

            【解决方案8】:
            public static void main(String args[])
            {
                    String string="2012-09-13"; 
                    Date str=processFileDate(string);
                    System.out.println(str);
            }
            public static Date processFileDate(String str)
            { //returns the date or "null" if doesn't exist
            
                String[] strformat={
                        "EEE,dd MMM yyyy","MMM dd, yyyy, hh.mmaa zzz",
                        "EEEE, MMMMM dd  yyyy 'at' hh:mm",
                        "EEEE, MMMMM dd, yyyy, hh:mm",
                        "EEE MMM dd yyyy, hh:mm ",
                        "dd MMMMM yyyy'Last updated at' hh:mm zzz",
                        "MMM dd, yyyy 'at' hh:mmaa",
                        "MMM dd, yyyy 'at'  hh:mmaa zzz",
                        "MMMMM dd, yyyy, hh:mm aa zzz",
                        "EEE, MMM dd, yyyy hh:mm ",
                        "MMMMM dd, yyyy hh:mm zzz",
                        "MMMM dd, yyyy hh:mm aa",
                        "MMMM dd, yyyy hh:mmaa",
                        "MMMM dd, yyyy hh:mm",
                        "dd MMMM yyyy hh:mm:ss",
                        "dd MMMM yyyy hh:mm",
                        "MMMM dd, yyyy",
                        "dd MMMM yyyy ",
                        "dd MM yy",
                        "yyyy MMMM dd",
                        "dd'st' MMMM,yyyy",
                        "dd'nd' MMMM,yyyy",
                        "dd'rd' MMMM,yyyy",
                        "MMMM dd,yyyy",
                        "MMM dd yy",
                        "mm dd yy",
                        "yyyy-MM-dd",
                        "yyyy-MM-dd HH:mm:ss",
                        "E MMM dd hh:mm:ss Z yyyy",
                        "EEE, dd MMM yyyy HH:mm:ss Z"
            
                };
            
                String temp="null";
                for(int i=0;i<str.length();i++){
                    temp=str.substring(i, str.length());
                    for(int l=0;l<strformat.length;){
                        Date strp=checkformat(temp,strformat[l]);
                        if(strp!=null)
                        {
                            return strp;
                        }
                        else l++;
                    }
                 }
                return null;
            }
            
            private static Date checkformat(String str, String sdf) {
                 SimpleDateFormat sdformat=new SimpleDateFormat(sdf);
            
                   try{
                    Date d=sdformat.parse(str);
                    return d;
                   }catch(Exception e){}
            
                   return null;
            }
            

            【讨论】:

              猜你喜欢
              • 2017-09-17
              • 1970-01-01
              • 2017-10-15
              • 2014-09-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-02-04
              相关资源
              最近更新 更多