1
我有這樣的代碼:SimpleDateFormat的解析返回錯誤值
public static String formatMinSecOrHourMinSec(final String length) {
try {
final SimpleDateFormat hhmmss = new SimpleDateFormat("HH:mm:ss", Locale.GERMAN);
final Date date = hhmmss.parse(length);
final GregorianCalendar gc0 = new GregorianCalendar(Locale.GERMAN);
gc0.setTime(date);
if(gc0.getTimeInMillis() >= 3600 * 1000){
return hhmmss.format(gc0.getTime());
}else{
final SimpleDateFormat mmss = new SimpleDateFormat("mm:ss");
return mmss.format(gc0.getTime());
}
} catch (final ParseException e) {
LOGGER.debug("Konnte die Länge nicht parsen: " + length + "\n" + e);
return length;
}
}
我估計,它返回1點29分00秒如果length
設置爲1點29分00秒但它返回29: 00。這是因爲gc0.getTimeInMillis()
比預期少返回一小時(3600 * 1000)。我究竟做錯了什麼 ?
gc0.getTimeInMillis()返回1小時以下(3600 * 1000),高於預期。 所以根據你的陳述你的條件是錯誤的,你得到的是正確的。 –
您可能會也可能不會遇到時區問題。您正在'Locale.German'中加載時間,但millis中的時間將作爲相對於UTC(格林威治標準時間)1-1-1970午夜的偏移量返回,這可能是導致意外小時偏移的原因,但是當然你需要考慮一些事情。 – Caleryn