簡體   English   中英

在java中將字符串轉換為日期時的意外輸出

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

我有一個字符串"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);      

但是,當我打印輸出時,我明白了

Thu Dec 09 04:39:38 IST 2010

如何按照我在字符串中聲明的方式獲取日期,即

12/9/2010 4:39:38 PM

作為輸出? 請幫忙

您假設Date值本身會記住格式 - 它不會。 Date.toString將按照自己的Date.toString - 因為Date只代表一個時刻。

如果要格式化 Date ,請再次使用格式化程序:

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

但是,這不一定會返回與字符串中完全相同的值,因為可能存在多個以相同方式解析的值。 例如,因為你只使用了“H:m:s”,我希望“4:5:6”的解析方式與“04:05:06”相同。

您可以使用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

使用相同的格式化程序:

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沒有內在格式的概念 - 您需要使用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));

不確定你想要完成什么。 但是你必須調用SimpleDateFormat.format()來獲得你期望的結果。 直接打印日期只會獲得Date的String()實現

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM