4

我正在使用 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

4

4 回答 4

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);
于 2013-09-24T07:57:43.723 回答
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);

在 dubug 中:hms 的值 = -4:-59:-51

于 2013-09-24T07:39:28.013 回答
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

于 2013-09-24T10:52:11.777 回答
0

hh 为 1~12。你应该使用 HH (0~23)

String timeinsec = "00:00:08";
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date time =  df.parse(timeinsec);
于 2013-09-24T08:04:48.210 回答