2011-04-29 131 views
2

我想將日期轉換爲其他格式。java中的日期格式

例如,

字符串FROM日期= 「2011-04-22」; 我想將其從日期轉換爲「2011年4月22日」

我該怎麼做?

由於提前

回答

1

您可以使用SimpleDateFormat分析給定的格式,然後使用寫第二形式的不同SimpleDateFormat

SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd"); 
SimpleDateFormat to = new SimpleDateFormat("dd MMM, yyyy"); 

Date dat = from.parse("2011-04-22"); 
System.out.println(to.format(dat)); 

不知道如何,如果有一種方法來添加「 nd'到'22nd'。

0
String dateString = "2011-04-22"; 
SimpleDateFormat format = new SimpleDateFormat("d MMM, yyyy"); 

try { 
    Date parsed = format.parse(dateString); 
} 
catch(ParseException pe) { 
    System.out.println("ERROR: Cannot parse \"" + dateString + "\""); 
} 
0

我想指出的是,格式應取決於區域設置......當然,你可以像這樣做:

String fromDate = "2011-04-22"; 
DateFormat incomming = new SimpleDateFormat("yyyy-MM-dd"); 
DateFormat outgoing = DateFormat.getDateInstance(DateFormat.Long, Locale.US); 
try { 
    Date parsed = incomming.parse(fromDate); 
    String toDate = outgoing.format(parsed); 
} 
catch (ParseException pe) { 
    pe.printStackTrace(); 
} 

當然,不是Locale.US的需要傳遞最終用戶的區域設置...

順便說一句。而不是糟糕的SimpleDateFormat,你可能想使用Apache Commons Lang'sFastDateFormat。如果您正在執行許多與日期相關的操作,還請查找DateUtils

+0

以區域設置的長格式輸出很有用,但不是OP所要求的。他們給出了他們需要的非常具體的輸出格式。 – Jason 2011-04-29 06:52:47

+0

我知道他問了具體的格式,從I18n的角度來看這可能是無效的。不是那麼長,默認應該用... – 2011-04-29 07:01:26

3

你想要的是有點棘手,因爲在22日的「nd」。根據當天的情況,它需要一個不同的後綴。 SimpleDateFormat不支持這樣的格式。你將不得不編寫一些額外的代碼來獲得它。下面是一個例子,但它僅限於在美國某些地區使用:

SimpleDateFormat fromFormat = new SimpleDateFormat("yyyy-MM-dd"); 
SimpleDateFormat toFormat = new SimpleDateFormat("d'__' MMM, yyyy"); 

String fromDate = "2011-04-22"; 
Date date = fromFormat.parse(fromDate); 
String toDate = toFormat.format(date); 

Calendar cal = Calendar.getInstance(); 
cal.setTime(date); 
int day = cal.get(Calendar.DAY_OF_MONTH); 
if (day % 10 == 1 && day != 11) { 
    toDate = toDate.replaceAll("__", "st"); 
} else if (day % 10 == 2 && day != 12) { 
    toDate = toDate.replaceAll("__", "nd"); 
} else if (day % 10 == 3 && day != 13) { 
    toDate = toDate.replaceAll("__", "rd"); 
} else { 
    toDate = toDate.replaceAll("__", "th"); 
} 

System.out.println(toDate); 
+0

thks先生你的幫助 – 2011-04-29 07:31:28

+0

沒問題。接受答案,如果它適合你:) – WhiteFang34 2011-04-29 07:34:20

1

按照此代碼刪除序號的日期。它正在成功運行。

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.regex.Pattern; 
import java.util.regex.Matcher; 

public class patrn { 

    private static String deleteOrdinal(String dateString) { 
     Pattern p1 = Pattern.compile("([0-9]+)(st|nd|rd|th)"); 
     Matcher m = p1.matcher(dateString); 
     while (m.find()) { 
      dateString = dateString.replaceAll(Matcher.quoteReplacement(m.group(0)), m.group(1)); 
     } 
     return dateString; 
    } 

    public static void main(String[] args) { 

     String dateString = "August 21st, 2012"; 
     SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd, yyyy"); 
     Date emp1joinDate = null; 
     try { 
      emp1joinDate = sdf.parse(deleteOrdinal(dateString)); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
}