Android,如何將字符串轉(zhuǎn)換為日期?每當(dāng)用戶啟動(dòng)應(yīng)用程序時(shí),我都會(huì)將當(dāng)前時(shí)間存儲在數(shù)據(jù)庫中。Calendar c = Calendar.getInstance(); String str = c.getTime().toString(); Log.i("Current time", str);在數(shù)據(jù)庫端,我將當(dāng)前時(shí)間存儲為字符串(如上面的代碼所示)。因此,當(dāng)我從數(shù)據(jù)庫加載它時(shí),需要將其強(qiáng)制轉(zhuǎn)換為Date對象。我看到了一些所有人都使用過“ DateFormat”的示例。但是我的格式與日期格式完全相同。因此,我認(rèn)為沒有必要使用“ DateFormat”。我對嗎?無論如何,有沒有直接將此String轉(zhuǎn)換為Date對象?我想將此存儲時(shí)間與當(dāng)前時(shí)間進(jìn)行比較。謝謝======> 更新謝謝親愛的家伙們。我使用以下代碼:private boolean isPackageExpired(String date){ boolean isExpired=false; Date expiredDate = stringToDate(date, "EEE MMM d HH:mm:ss zz yyyy"); if (new Date().after(expiredDate)) isExpired=true; return isExpired; } private Date stringToDate(String aDate,String aFormat) { if(aDate==null) return null; ParsePosition pos = new ParsePosition(0); SimpleDateFormat simpledateformat = new SimpleDateFormat(aFormat); Date stringDate = simpledateformat.parse(aDate, pos); return stringDate; }
- 3 回答
- 0 關(guān)注
- 883 瀏覽
添加回答
舉報(bào)
0/150
提交
取消