2014-10-09 125 views
1

我有这样的日期时间09/01/2014,我想格式化这个日期时间像这样9月1日。我写了一些代码,我可以在这个代码结果格式此日期这样安卓字符串日期时间格式与月和日

     SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy"); 

         Date _d = df.parse("09/01/2014");     
         SimpleDateFormat new_df = new SimpleDateFormat(
           "dd"); 
         String _s = new_df.format(_d); 
         cinemaTime.setStartTime(_s); 

是onlu 01,但我不知道我可以recive 9月1日(年9个月( 如果有人知道的解决方案,请帮我感谢

+0

什么正是要接收的格式? “9月1日(9月)(”??? – Jorgesys 2014-10-09 22:25:37

回答

3

如果您需要像“2014年9月1日”这样的输出,此方法将执行此项工作。

public static String dateFormatterforLukka(){  
    String inputDate = "09/01/2014";   
    String inputFormat = "MM/dd/yyyy"; 
    String outputFormat = "d ' ' MMMM ' ' yyyy"; 

    Date parsed = null; 
    String outputDate = ""; 
    try { 
     SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, new Locale("en", "US")); 
     SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, new Locale("en", "US"));      
     parsed = df_input.parse(inputDate); 
     outputDate = df_output.format(parsed); 
    } catch (Exception e) { 
     outputDate = inputDate;  
    } 
    return outputDate; 
} 

阅读有关文件:SimpleDateFormat

0

你只得到“01”,因为这就是你问它给你("dd")。MMMM会给你每月的词。如果你想只是“1”,而不是“01” ,您只需使用d而不是dd

进一步文档:http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

+0

问题解决。这是一个解决方案SimpleDateFormat new_df = new SimpleDateFormat(“d MMM”);谢谢。但它是可以改变语言? – lukaaa 2014-10-09 22:28:53

+0

你应该能够使用'new SimpleDateFormat(“d MMM”,Locale.FRENCH)添加'Locale';' – ipavl 2014-10-09 22:34:35

+0

语言环境的文档:http://docs.oracle.com/javase/7/docs/api/java/util/Locale。 HTML#field_summary – ipavl 2014-10-09 22:35:12