Java不可破解的日期 [英] Java Unparsable date

查看:231
本文介绍了Java不可破解的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串格式: String dateString =2014-03-17T20:05:49.2300963Z



尝试这样:

  SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd'Tkk:mm: ss.SSSX); 
Date date = df.parse(dateString);

结果无法解析的日期 exceptioon。 / p>

文档: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html 表示 X 是与单个字母用于TimeZone时,与 ISO 8601 一起使用。



编辑
重新阅读文档,我已经启动了 SimpleDateFormat 一点:

  SimpleDateFormat df = new SimpleDateFormat(yyyy-MM-dd'T'HH:mm:ss .SSS); 
dateString = dateString.replace(Z,);

我取出了 Z ,因为我知道时区,使用 H 而不是 k 并添加几个 S 为giggles。



现在时间是解析,但不正确。日期是准确的,时间似乎是随机的。



编辑2
问题是java只允许毫秒的准确性,所以 2300963 被解释为2300秒和963毫秒。我需要格式化我的字符串,以使其正常工作。



编辑3
结果你可以'在Java中有一小部分时间。它必须被截断为毫秒。我最终使用了我的数据库提供给我的类型,但一般的解决方案是将第二部分的小数部分截断为毫秒。我将把如何做到这一点的示例代码作为答案。

解决方案

如何将小数秒缩短到毫秒(因为Java不能处理分秒):

  public String truncate(String dateString){
int numberOfDigits = dateString。 substring(dateString.indexOf(。),dateString.length() - 1).length();

String justMilliSecondsDate = null;
if(numberOfDigits == 3){
justMicrosDate = dateString;
}
else if(numberOfDigits> 3){
justMilliSecondsDate = dateString.substring(0,dateString.length() - numberOfDigits + 3);
}
else {
justMilliSecondsDate = dateString; (int i = numberOfDigits; i< 3; i ++)
justMilliSecondsDate + =0;
}

return justMilliSecondsDate;
}


I have a string with the format: String dateString = "2014-03-17T20:05:49.2300963Z"

Trying this:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSSX");
Date date = df.parse(dateString);

Results in an Unparsable date exceptioon.

The docs: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html indicate that X is used with ISO 8601 when a single letter is used for the TimeZone.

EDIT Re-reading the docs, I've switched up the SimpleDateFormat a little:

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateString = dateString.replace("Z", "");

I take out the Z because I know the timezone, use H instead of k and add a couple more S for giggles.

Now the time is parsing, but incorrectly. Date is accurate, Time seems to be random.

EDIT 2 The problem is that java only allows millisecond accuracy, so 2300963 is being interpreted as 2300 seconds and 963 milliseconds. I'll need to format my string a little differently to get this to work.

EDIT 3 Turns out you can't have a fractional part of a second in Java. It has to be truncated to milliseconds. I ended up using a type made available to me by my database, but the general solution is to truncate the fractional part of the second to millisecond. I'll post example code of how to do that as an answer.

解决方案

How to truncate the fractional seconds to milliseconds (because Java can't handle fractional seconds):

public String truncate(String dateString){
  int numberOfDigits = dateString.substring(dateString.indexOf("."), dateString.length() - 1).length();

  String justMilliSecondsDate = null;
  if (numberOfDigits == 3) {
    justMicrosDate = dateString;
    } 
  else if (numberOfDigits > 3) {
    justMilliSecondsDate = dateString.substring(0, dateString.length() - numberOfDigits + 3);
  } 
  else {
      justMilliSecondsDate = dateString;
      for (int i = numberOfDigits ; i < 3 ; i++) justMilliSecondsDate += "0";
  }

  return justMilliSecondsDate;
}

这篇关于Java不可破解的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆