我有一个包含日期和时间信息的JSON对象,我想检索数据并根据日期/时间设置提醒。
{ “状态”:200, “消息”: “成功”, “结果”:[{ “Remainderid”: “1” “剩余”:“生日”,“日期”:“2017-06-13 10:00:00”}]}
和我的代码
private void getReminder() {
String url = Urls.current_date_view + "&Userid=" + Model.getInstance().userid;// Model.getInstance().userid;
Log.e("RemindersUrl====>", "" + url);
AsyncHttpClient client = new AsyncHttpClient();
client.get(getApplicationContext(), url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
reminsList = new ArrayList<HashMap<String, String>>();
reminsList.clear();
try {
reminresponse = new JSONObject(new String(responseBody));
if (reminresponse == null) {
Toast.makeText(getApplicationContext(), "Please Check Internet Connection", Toast.LENGTH_SHORT).show();
} else {
/*{"status":200,"message":"Success","result":[{,"Remainderid":"1"
"Remainder":"Birthday","Date":"2017-06-13 10:00:00"}]}*/
remind_status = reminresponse.optString("status");
reminstatus_msg = reminresponse.optString("message");
if (remind_status.equalsIgnoreCase("200")) {
JSONArray array = reminresponse.getJSONArray("result");
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String mTime = "", mDate = "";
String mIDs = object.getString("Remainderid");
String mTitle = object.getString("Remainder");
String curDate = object.getString("Date");
Calendar calendars = Calendar.getInstance();
Log.e("DATEANDmIDs====>", " " + mIDs + " ");
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date datemniles = f.parse(curDate);
calendars.setTime(datemniles);
mHour = calendars.get(Calendar.HOUR_OF_DAY);
mMinute = calendars.get(Calendar.MINUTE);
mYear = calendars.get(Calendar.YEAR);
mMonth = calendars.get(Calendar.MONTH);
mDay = calendars.get(Calendar.DATE);
mDate = mDay + "-" + mMonth + "-" + mYear;
mTime = mHour + ":" + mMinute;
mCalendar.set(Calendar.MONTH, mMonth);
mCalendar.set(Calendar.YEAR, mYear);
mCalendar.set(Calendar.DAY_OF_MONTH, mDay);
mCalendar.set(Calendar.HOUR_OF_DAY, mHour);
mCalendar.set(Calendar.MINUTE, mMinute);
mCalendar.set(Calendar.SECOND, mSec);
saveReminder(mIDs, mTitle, mTime, mDate);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
SetRemainder代码
public void saveReminder(String mIDs, String mTitle, String mTime, String mDate) {
Intent intents=getIntent();
int ID;
String reminder="";
// Check repeat type
reminders = rb.getAllReminders();
Log.e("remindersALL===>", "" + reminders);
if (reminders.isEmpty()) {
//AddSqliteDataBase
ID = rb.addReminder(new Reminder_set(Integer.parseInt(mIDs), mTitle, mDate, mTime));
getAlarams(ID);
} else if (!reminders.isEmpty()) {
for (int i = 0; i < reminders.size(); i++) {
reminder = String.valueOf(reminders.get(i).getID());
}
if (reminder.equalsIgnoreCase(mIDs+1)) {
Log.e("WorkEquals===>", "" + reminder);
getUpdates(String.valueOf(Integer.parseInt(mIDs)), mTitle, mDate, mTime);
} else {
ID = rb.addReminder(new Reminder_set(Integer.parseInt(mIDs), mTitle, mDate, mTime));
Log.e("NotEqualsID===>", "" + reminder+"==="+mIDs+1);
getAlarams(ID);
}
}
}
private void getAlarams(int ID) {
Log.e("Alaram==>",""+"Workesd ==>" +ID);
new AlarmReceiver().setAlarm(getApplicationContext(), mCalendar, ID);
}
private void getUpdates(String mIDs, String mTitle, String mTime, String mDate) {
mReceivedReminder = rb.getReminder(Integer.parseInt(mIDs));
mReceivedReminder.setID(Integer.parseInt(mIDs));
mReceivedReminder.setTitle(mTitle);
mReceivedReminder.setDate(mDate);
mReceivedReminder.setTime(mTime);
rb.updateReminder(mReceivedReminder);
}
答案 0 :(得分:1)
试试这个
创建一个json对象
JSONObject data = getJSONObject(response);
获取特定字符串
String date = data.getString("Remindercurdate");
String Reminder = data.getString("Reminder");
现在将String转换为Date,就像这样
String mydate = date;// your date from json
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
Date Resultdate = format.parse(mydate);
Log.e("Date id ",""+Resultdate);
} catch (ParseException e) {
e.printStackTrace();
}