我正在使用Java日期,我无法摆脱这个问题。 在我的文件中,时间值保存为(HH:MM:SS)
00:00:08 下面是代码和输出..
String timeinsec = "00:00:08";
DateFormat df = new SimpleDateFormat("hh:mm:ss");
Date time = df.parse(timeinsec);
当我分配值和时间变量时发生了什么。 time.fastTime变量显示 “-17992000”
当我将此值转换回HH:MM:SS时,它会显示给我。 “-4:-59:-51”
任何人都可以帮助解决TimeZone问题。 我目前的时区是GMT + 5
答案 0 :(得分:3)
试试这个;
int day = (int) TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day * 24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60);
long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60);
答案 1 :(得分:2)
这是转换代码:
Date new_time = Time_array.get(0).time; //-17992000 stored in "fastTime" variable
long diff = ((long)new_time.getTime()); //TimeUnit.MILLISECONDS
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
String hms = String.format("%d:%02d:%02d", diffHours, diffMinutes, diffSeconds);
在调试中:hms = -4:-59:-51
的值答案 2 :(得分:2)
我正在尝试各种方式,所以最后我写了这段代码,我的要求得以实现。
Calendar cal = Calendar.getInstance();
String timeinsec = "00:00:08";
DateFormat df = new SimpleDateFormat("hh:mm:ss");
Date time = df.parse(timeinsec);
cal.setTime(time);
hms = String.format("%d:%02d:%02d", cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
输出为:00:00:08
答案 3 :(得分:0)
hh是1~12。你应该使用HH(0~23)
String timeinsec = "00:00:08";
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date time = df.parse(timeinsec);