下面列出了android.text.format.Time#parse3339 ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Time timeFrom3339(String s3339) {
final Time time = new Time();
if (TextUtils.isEmpty(s3339)) {
return time;
}
try {
time.parse3339(s3339);
} catch (TimeFormatException e) {
return time;
}
time.switchTimezone(Time.getCurrentTimezone());
return time;
}
/**
* Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them
* off to their respective "read" methods for processing. Otherwise, skips the tag.
*/
private Entry readEntry(XmlPullParser parser)
throws XmlPullParserException, IOException, ParseException {
parser.require(XmlPullParser.START_TAG, ns, "entry");
String id = null;
String title = null;
String link = null;
long publishedOn = 0;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("id")){
// Example: <id>urn:uuid:218AC159-7F68-4CC6-873F-22AE6017390D</id>
id = readTag(parser, TAG_ID);
} else if (name.equals("title")) {
// Example: <title>Article title</title>
title = readTag(parser, TAG_TITLE);
} else if (name.equals("link")) {
// Example: <link rel="alternate" type="text/html" href="http://example.com/article/1234"/>
//
// Multiple link types can be included. readAlternateLink() will only return
// non-null when reading an "alternate"-type link. Ignore other responses.
String tempLink = readTag(parser, TAG_LINK);
if (tempLink != null) {
link = tempLink;
}
} else if (name.equals("published")) {
// Example: <published>2003-06-27T12:00:00Z</published>
Time t = new Time();
t.parse3339(readTag(parser, TAG_PUBLISHED));
publishedOn = t.toMillis(false);
} else {
skip(parser);
}
}
return new Entry(id, title, link, publishedOn);
}
@Override
protected void onHandleIntent(Intent intent) {
Time time = new Time();
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null || !ni.isConnected()) {
Log.w(TAG, "Not online, not refreshing.");
return;
}
sendStickyBroadcast(
new Intent(BROADCAST_ACTION_STATE_CHANGE).putExtra(EXTRA_REFRESHING, true));
// Don't even inspect the intent, we only do one thing, and that's fetch content.
ArrayList<ContentProviderOperation> cpo = new ArrayList<ContentProviderOperation>();
Uri dirUri = ItemsContract.Items.buildDirUri();
try {
JSONArray array = RemoteEndpointUtil.fetchJsonArray();
if (array == null) {
throw new JSONException("Invalid parsed item array");
}
for (int i = 0; i < array.length(); i++) {
ContentValues values = new ContentValues();
JSONObject object = array.getJSONObject(i);
values.put(ItemsContract.Items.SERVER_ID, object.getString("id"));
values.put(ItemsContract.Items.AUTHOR, object.getString("author"));
values.put(ItemsContract.Items.TITLE, object.getString("title"));
values.put(ItemsContract.Items.BODY, object.getString("body"));
values.put(ItemsContract.Items.THUMB_URL, object.getString("thumb"));
values.put(ItemsContract.Items.PHOTO_URL, object.getString("photo"));
values.put(ItemsContract.Items.ASPECT_RATIO, object.getString("aspect_ratio"));
time.parse3339(object.getString("published_date"));
values.put(ItemsContract.Items.PUBLISHED_DATE, time.toMillis(false));
Long id = Long.valueOf(object.getString("id"));
Cursor cursor = getContentResolver().query(ItemsContract.Items.buildDirUri(),
ArticleLoader.Query.PROJECTION, ItemsContract.Items.SERVER_ID + "=" + id,
null, null);
if (cursor == null || !cursor.moveToFirst()) {
cpo.add(ContentProviderOperation.newInsert(dirUri).withValues(values).build());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
getContentResolver().applyBatch(ItemsContract.CONTENT_AUTHORITY, cpo);
} catch (JSONException | RemoteException | OperationApplicationException e) {
Log.e(TAG, "Error updating content.", e);
}
sendStickyBroadcast(
new Intent(BROADCAST_ACTION_STATE_CHANGE).putExtra(EXTRA_REFRESHING, false));
}
protected Time parseDate(String sms, String datePattern, Ticket ticket) throws ParseException {
Matcher m = Pattern.compile(datePattern).matcher(sms);
if (m.find()) {
String d = m.group(1);
if (!TextUtils.isEmpty(d)) {
d = d.replaceAll(";", "");
for (int i = 0; i < 2; i++) {
final Date date;
try {
if (i == 0) {
date = sdf.parse(d); // full date/time
} else if (i == 1 && sdfTime != null) {
date = sdfTime.parse(d); // only time
} else {
break;
}
} catch (Exception e) {
continue;
}
if (i == 1 && ticket != null && ticket.getValidFrom() != null) {
final Date prevDate = new Date(ticket.getValidFrom().toMillis(true));
date.setYear(prevDate.getYear());
date.setMonth(prevDate.getMonth());
date.setDate(prevDate.getDate());
}
final Calendar c = Calendar.getInstance();
c.setTime(date);
final Time t = new Time();
synchronized (format3339) {
String zone = timezone.format(date);
zone = zone.substring(0, 3) + ":" + zone.substring(3);
String s = format3339.format(date) + zone;
t.parse3339(s);
t.switchTimezone(Time.getCurrentTimezone());
}
return t;
}
}
}
throw new ParseException("Cannot parse date from the message: " + sms, 0);
}
public static Date parse3339(String time) {
final Time t = new Time();
t.parse3339(time);
return new Date(t.toMillis(false));
}