2012-06-15 150 views
0

有谁知道如何格式化包含日期的字符串?安卓日期格式化字符串

我有一个字符串,其中包含从Json订阅源传递的日期和时间。

它看起来像这样

2012-06-11 14:00 

这个问题是它以错误的方式如何,使其成为两个字符串,所以我可以插图中例如添加位做我格式化。有谁知道我该怎么做?

"at " + time + " " + "on " + "11-06-2012" 

at 14:00 on 11-06-2012 
+0

简单的日期格式解析和格式的方法会帮助你http://developer.android.com/reference/java/text/SimpleDateFormat.html –

回答

3

试试这个..

SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd hh:mm"); 
java.util.Date date = null; 
try 
{ 
    date = form.parse(string); 
} 
    catch (ParseException e) 
{ 
} 
SimpleDateFormat postFormater = new SimpleDateFormat("dd-MM-yyyy"); 
String newDateStr = "at"+date.getHours()+":"+date.getMinutes()+"on"+postFormater.format(date); 
0
 try { 
      SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 
      SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("'at' HH:mm 'on' dd-MM-yyyy"); 
      simpleDateFormat2.format(simpleDateFormat1.parse("2012-06-11 14:00")); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     }