時(shí)間轉(zhuǎn)換有關(guān)問題:從格式為"yyyy-MM-dd"的日期轉(zhuǎn)換成距離1970年1月1日的毫秒數(shù),可再從得到的毫秒數(shù)如何也轉(zhuǎn)換不回去原來的日期(2)
www.MyException.Cn 發(fā)布于:2011-12-29 22:09:38 瀏覽:27次
------解決方案-------------------------------------------------------- import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class IQAUtil { public static int getSecondsFromDate(String expireDate) { if (expireDate == null || expireDate.trim().equals(" ")) return 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "); Date date = null; try { date = sdf.parse(expireDate); return (int) (date.getTime() / 1000); } catch (ParseException e) { e.printStackTrace(); return 0; } } public static String getDateFromSeconds(String seconds) { if (seconds == null) return " "; else { Date date = new Date(); try { date.setTime(Long.parseLong(seconds) * 1000); } catch (NumberFormatException nfe) { nfe.printStackTrace(); return "Input string: " + seconds + " not correct, eg:2007-01-31 "; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "); return sdf.format(date); } } public static String getDateFromMillisSeconds(String millis) { if (millis == null) return " "; else { Date date = new Date(); try { date.setTime(Long.parseLong(millis)); } catch (NumberFormatException nfe) { nfe.printStackTrace(); return "Input string: " + millis + " not correct, eg:2007-01-31 "; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); return sdf.format(date); } } public static void main(String[] arg0) { // int i = IQAUtil .getSecondsFromDate( "2007-01-31 "); long t = System.currentTimeMillis(); long i = t / 1000; long j = t % 1000; System.out.println(i); String str = IQAUtil .getDateFromSeconds(String.valueOf(i)); System.out.println(str); str = IQAUtil .getDateFromMillisSeconds(String.valueOf(t)); System.out.println(str); } } ------解決方案-------------------------------------------------------- 樓上的結(jié)果就很正確了,為什么你從毫秒變?yōu)閅YYY-MM-dd的時(shí)候知道用data.setTime();但是從YYYY-MM-dd的時(shí)候卻不用getTime()呢? getTime public long getTime()返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數(shù)。 返回: 自 1970 年 1 月 1 日 00:00:00 GMT 以來此日期表示的毫秒數(shù)。 -------------------------------------------------------------------------------- setTime public void setTime(long time)設(shè)置此 Date 對象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的時(shí)間點(diǎn)。 參數(shù): time - 毫秒數(shù)。 現(xiàn)成的方法,不用自己寫 ------解決方案-------------------------------------------------------- ------------- 2007-04-31 ------------- 2007年的4月是沒有31號的,系統(tǒng)沒有算錯(cuò),04-31就是05-01。。。。 ------解決方案-------------------------------------------------------- 當(dāng)我傳入入值為 "2007-04-31 "字串時(shí)返回給我的結(jié)果是 "2007-05-01 " ================================================================ 我想這種情況你只能自己在程序中控制了,你得保證你傳給date的是一個(gè)看上去合法合情合理的數(shù)據(jù),check也很中要啊,這兩個(gè)方法很不錯(cuò)了,給你轉(zhuǎn)成05-1,你傳一個(gè)沒有的值04-31,它肯定不會原樣給你返回,如果是人,你說04-31,他也會告訴你那事5-1 ------解決方案-------------------------------------------------------- eyesdragon() ( ) 信譽(yù):97 Blog 2007-3-23 23:28:29 得分: 0 謝謝兩位,不過我測了zhuokai() 給我改后的代碼跟我之前的一樣得不到正確結(jié)果,當(dāng)我傳入入值為 "2007-04-31 "字串時(shí)返回給我的結(jié)果是 "2007-05-01 ",而傳入 "2007-03-31 "時(shí)返回的結(jié)果就時(shí)正確的,多試幾次后發(fā)現(xiàn)時(shí)而正確時(shí)而不正確.還有誰有更好的建議?先謝謝各位啦 ========== month, day 最好不要直接用整數(shù),因?yàn)橐辉率? 。 用你的方法設(shè)置日期,保存的結(jié)果是month ,day對12,當(dāng)月天數(shù)取余之后的日期(也向前進(jìn)位)。 比如,2007-01-31 = 2007年2月31日 但2月沒有31日,多出來了3天,所以 2007-01-31 = 2007年3月3日 2007-12-31 = 2008年1月31日 ------解決方案-------------------------------------------------------- 這個(gè)問題不是你處理錯(cuò)了,而是API理解錯(cuò)了。 如果你可以在 getSecondsFromDate 中設(shè)置了日期后加一句 System.out.println(c.getTime()); 就可以很清楚看到問題所在了。 ------解決方案-------------------------------------------------------- 同意上面的確說法 |
|