从安卓8开始,引入了OffsetDateTime类,所以在安卓8及以上时,就可以使用OffsetDateTime类了。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
OffsetDateTime time = OffsetDateTime.parse("2019-08-24T14:15:22Z");
bd.supermarketTvTime.setText(time.getYear() + "/" + time.getMonthValue() + "/" + time.getDayOfMonth());
} else {
try {
SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
Date time = mSimpleDateFormat.parse("2019-08-24T14:15:22Z");
Calendar calendar = Calendar.getInstance();
calendar.setTime(time);
// 获取当前年
int year = calendar.get(Calendar.YEAR);
// 获取当前月
int month = calendar.get(Calendar.MONTH) + 1;
// 获取当前日
int day = calendar.get(Calendar.DATE);
bd.supermarketTvTime.setText(year + "/" + month + "/" + day);
} catch (ParseException e) {
}
}