2014-11-08 82 views
1

在我的android应用程序中,我使用JSON解析mysql数据库中的数据并在android listview中显示。在MySQL数据库中,日期格式为YYYY-MM-DD,我在android中使用JSON作为字符串获取此格式。是否有可能在Android中更改日期格式为DD-MM-YYYY而不是数据库.. ??如何更改来自数据库的日期格式为android

回答

1

使用SimpleDateFormat一个格式转换日期的形式为另一种格式:

SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD"); 
SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY"); 
try{ 
    Date d = df1.parse(DateString); 
    System.out.println("new date format " + df2.format(d)); 
}catch(Exception e){ 
    e.printStackTrace(); 
} 
+0

:) Thanx .. !!!你再次保存了我的时间.. :) – Ruchir 2014-11-08 05:01:05

+0

@很高兴帮助你... – 2014-11-08 05:06:40

1

是的。使用SimpleDateFormat

String inputPattern = "yyyy-MM-dd"; 
String outputPattern = "dd-MM-yyyy"; 
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern); 
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern); 

Date date = null; 
String result = null; 

try { 
    date = inputFormat.parse(time); 
    result = outputFormat.format(date); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

//use the result variable as you wish 
+0

请检查您的输出格式是错误的,因为每个问题所需的DD-MM-YYYY,你用dd-MMM -YYYY。 – 2014-11-08 04:53:43

+0

@HareshChhelana谢谢:) – 2014-11-08 05:00:57