簡體   English   中英

使用 JAVA 將日期字符串“Thu Oct 15 05:19:21 XXX 2015”轉換為“yyyy-MM-dd”

[英]Convert date String "Thu Oct 15 05:19:21 XXX 2015" to "yyyy-MM-dd" with JAVA

我是 Java 新手,我正在嘗試制作格式轉換日期

我想要一個日期,“Thu Oct. 15 2015 5:19:21 XXX 2015”字符串類型......這種格式“yyyy-MM-dd”

嘗試“SimpleDateFormat”但沒有奏效。 導致“無法解析的日期”

      Input:
          String = "Thu Oct 15 05:19:21 XXX 2015"

      Output:
          String="2015-10-15"

對不起我的英語不好...

這是我的代碼:

public static String formatDate(String string) {
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date date;
    try {
        date = format.parse(string);
        getDateFormated(date, false);
        return getDateFormated(date, false);
    } catch (ParseException ex) {

    }
    return "";
} 

public static String getDateFormated(Date date,boolean withFormatHour) {
    if (date != null) {
        String format = "yyyy-MM-dd";
        if (withFormatHour) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        return new SimpleDateFormat(format).format(date);
    }
    return "";
} 

非常感謝!!!

你解析它並重新格式化它:

String input = "Thu Oct 15 05:19:21 XXX 2015";
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss 'XXX' yyyy").parse(input);
String output = new SimpleDateFormat("yyyy-MM-dd").format(date);

暫無
暫無

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

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