我是android的新手,我被困在一个地方。我想从mysql
数据库中获取日期并在android app中显示。
Android应用程序的代码
try {
JSONArray data = response.getJSONArray("std");
String result[]=new String[data.length()];
for (int i = 0; i < data.length(); i++)
{
JSONObject obj = data.getJSONObject(i);
name = obj.getString("Name");
fromdate=obj.getString("From");
todate = obj.getString("To");
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Calendar cal=Calendar.getInstance();
Date fd=formatter.parse(fromdate);
cal.setTime(fd);
from1=cal.getTimeInMillis();
Date td=formatter.parse(todate);
cal.setTime(td);
to1=cal.getTimeInMillis();
date= DateUtils.formatDateRange( Main2Activity.this, from1, to1,
DateUtils.FORMAT_SHOW_YEAR );
result[i]=""+name+"\n"+date;
}
JSON FILE
<?php
$result=mysqli_query($link,"select * from oTrip");
$data=array();
$rows=mysqli_num_rows($result);
while($rec=mysqli_fetch_assoc($result))
{
$data[]=$rec;
}
header('Content-Type: application/json');
echo json_encode(array("std" =>$data));
?>
输出
请帮忙! 谢谢。
答案 0 :(得分:0)
改变&#34; dd-MM-yyyy&#34;到&#34; dd / MM / yyyy&#34;
答案 1 :(得分:0)
你可以这样做,
fromdate=obj.getLong("From");
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
sdf.format(new Date(fromdate));
答案 2 :(得分:0)
使用此:
JSONObject obj = data.getJSONObject(i);
name = obj.getString("Name");
fromdate=obj.getString("From");
todate = obj.getString("To");
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date fd=formatter.parse(fromdate);
Date td=formatter.parse(todate);
DateFormat newFormatter = new SimpleDateFormat("dd-MM-yyyy");
String newFromdate = newFormatter.format(fd);
String newTodate = newFormatter.format(td);
date = newFromdate + " - " + newTodate;
result[i]=""+name+"\n"+date;
您必须以给定的JSON
格式解析日期,然后使用其他formatter
将解析后的日期更改为新格式。
您可以使用单行:
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
DateFormat newFormatter = new SimpleDateFormat("dd-MM-yyyy");
fromdate = newFormatter.format(formatter.parse(fromdate));
todate = newFormatter.format(formatter.parse(todate));