9

我有一个字符串类型的时间戳,我正在尝试将其转换为双精度(并在几秒钟内找到结果),这就是我所做的:

double mytimeStamp = 0;

String timeStamp = new SimpleDateFormat(" mm ss S").format(new Date( ));   

SimpleDateFormat dateFormat = new SimpleDateFormat(" mm ss S");

try {
  mytimeStamp = ((double)dateFormat.parse(timeStamp).getTime())/1000;
} catch (ParseException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
}

System.out.println("timeStamp is: "+ mytimeStamp);

问题是我获得了一个值-2722.515,我不知道为什么。

为什么是负面的?

代码有问题吗?

当我将此时间戳转换为mm ss S与实时不匹配时,这似乎是另一个问题!

4

2 回答 2

15

这是时区差异问题。

由于您只指定了分钟和秒,因此日期将打开1 Jan 1970 00:mm:ssmm并且ss是当前时间的分钟和秒)。

我将您的示例简化为:

String timeStamp = "00 00 00";
SimpleDateFormat dateFormat = new SimpleDateFormat("HH mm ss");
double hour = dateFormat.parse(timeStamp).getTime()/1000.0/60/60;
System.out.println("hour is: "+ hour);

打印出来的小时应该是GMT与当地时区的偏移量。

这样做的原因是:

SimpleDateFormat是区域敏感的,因此将返回为给定时区dateFormat.parse(timeStamp)创建对象(默认为本地时区)。Date然后getTime()从 获取毫秒数midnight 1 Jan 1970 **GMT**。因此,该值将被本地时区与多远的距离所抵消GMT

如何修复它:

dateFormat您可以通过在调用之前设置对象的时区来修复它,parse如下所示:

dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
于 2013-02-21T13:44:59.750 回答
1

---实际上有一个更好的方法可以做到这一点,但如果你想使用日期,请跳到预编辑答案---

日期实际上并没有按照您的意愿行事,这似乎是在实际需要从现实世界日历中挑选时间之外的时间计算。

你最好编写自己的类,以避免 Dates 为了跟上公历而必须做的所有讨厌的特殊处理。这种特殊处理包括(但不限于)时区意识、夏令时、声明的“跳过的日子”、闰秒、闰年等。

public TimeOnly {

   private long timestamp;
   private int millis;
   private int seconds;
   ... etc ...

   public TimeOnly(int hours, int minutes, int seconds, int millis) {
     this.timestamp = millis + seconds * 1000L + minutes * 60000L + hours * 3600000L; 
     this.millis = millis;
     this.seconds = seconds;
     ... etc ...
   }

   private TimeOnly(long timestamp) {
     this.timestamp = timestamp;
     this.millis = timestamp % 1000;
     this.seconds = timestamp % 60000L - this.millis;
     ... etc ...
   }

   public long getTimestamp() {
     return timestamp;
   }

   public int getMillis() {
     return millis;
   }

   public int getSeconds() {
     return seconds;
   }

   ... etc ...
}

public TimeFormatter {

  public TimeFormatter() {

  }

  public String format(Time time) {
    StringBuilder builder = new StringBuilder();
    builder.append(String.valueOf(time.getHours()));
    builder.append(":");
    builder.append(String.valueOf(time.getMinutes()));
    builder.append(":");
    builder.append(String.valueOf(time.getSeconds()));
    builder.append(".");
    if (time.getMillis() < 10) {
      builder.append("00");
    } else if (time.getMillis() < 100) {
      builder.append("0");
    }
    builder.append(time.getMillis());
    return builder.toString();
}

这个解决方案看起来像是在重新发明轮子,但实际上它是在避免使用八角形作为轮子。Date 的行为似乎不是您想要的,尽管您可以使 Date 适用于某些有限的值范围。

如果您想变得真正花哨,可以使上述工具具有可比性,等等。但是,我建议不要这样做。不要在构造后提供更新方法,因为这会强制进行一些非常讨厌的重新计算并使代码更难维护。而是提供返回新 TimeOnlys 以响应您希望实现的操作的方法。

public TimeOnly addSeconds(int value) {
  int stamp = this.timestamp;
  stamp += value * 60000L;
  if (stamp < timestamp) {
    throw new Excepton("overflow");
  }
  return new TimeOnly(stamp);
}

另外,不要实现你不会使用的东西。未使用的代码往往是滋生错误的沃土。

当然,所有“时间”事物的标准答案,考虑使用 JodaTime,它区分所有不同类型的时间测量。但是,对于这样一个小问题,它类似于使用坦克杀死一只蚂蚁。

---预编辑答案---

如果没有完整的时间规范(年、月、日、小时、分钟、秒、毫秒),您在第一步中格式化的时间值将有很多未指定的字段。这些领域的内容很可能是垃圾。

然后getTime()作用于整个Date对象,将有效字段和垃圾转换为一个值,其中垃圾甚至可以修改有效值(96 秒 = 1 分 36 秒,因为字段交互)。

解决此问题的最佳方法是将所有“仅时间”日期初始化为一个已知日期,因此当您进行比较和数学运算(是,3 11 23> 1 02 10?)时,您会得到一致的结果(是的,3 11 23> 1 02 10,因为它实际上是2013 02 10 00 03 11 23>2013 02 10 00 03 11 23而不是2013 02 10 00 03 11 23比较2000 02 10 00 03 11 23

选择使用日期时,请避开 2 月 29 日附近的日子、接近夏令时的日子等。

于 2013-02-21T13:56:53.353 回答