简体   繁体   English

在java中将字符串转换为日期时的意外输出

[英]unexpected output while converting a string to date in java

I have a string "12/9/2010 4:39:38 PM" which i have to convert to a date object. 我有一个字符串"12/9/2010 4:39:38 PM" ,我必须转换为日期对象。 I am using the following code to do it: 我使用以下代码来执行此操作:

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);      

However, when im printing the output, i see 但是,当我打印输出时,我明白了

Thu Dec 09 04:39:38 IST 2010 Thu Dec 09 04:39:38 IST 2010

How do I get the date exactly the way I declared in the string ie 如何按照我在字符串中声明的方式获取日期,即

12/9/2010 4:39:38 PM 12/9/2010 4:39:38 PM

as output? 作为输出? Pls help 请帮忙

You're assuming that the Date value itself remembers the format - it doesn't. 您假设Date值本身会记住格式 - 它不会。 Date.toString will do what it wants - because the Date only represents an instant in time. Date.toString将按照自己的Date.toString - 因为Date只代表一个时刻。

If you want to format a Date , use your formatter again: 如果要格式化 Date ,请再次使用格式化程序:

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

However, that won't necessarily return the exact same value that was in your string, as there may be multiple values which parse the same way. 但是,这不一定会返回与字符串中完全相同的值,因为可能存在多个以相同方式解析的值。 For example, as you've only used "H:m:s", I'd expect "4:5:6" to be parsed the same way as "04:05:06". 例如,因为你只使用了“H:m:s”,我希望“4:5:6”的解析方式与“04:05:06”相同。

You can entirely specify the format of your date output using the class Formatter 您可以使用Formatter类完全指定日期输出的格式

Short answer 简短的回答

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);

Other examples 其他例子

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

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

使用相同的格式化程序:

System.out.println("date printed "+ formatter.format(date));
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;
}

使用SimpleDateFormat将其转换回String?

formatter = new SimpleDateFormat("MM/dd/yyyy HH:m:ss a");
String temp =formatter.format(date ); 

Java.util.Date has no concept of an intrinsic format - You need to use the format(java.util.Date d) method to see a formatted String representation of your Date object. 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));

Not sure what you are trying to accomplish. 不确定你想要完成什么。 But you'll have to call SimpleDateFormat.format() to get what you are expecting. 但是你必须调用SimpleDateFormat.format()来获得你期望的结果。 Printing the date directly will get only toString() implementation of Date 直接打印日期只会获得Date的String()实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM