简体   繁体   English

将Java String转换为sql.Timestamp

[英]Convert Java String to sql.Timestamp

Got a String coming in with this format: YYYY-MM-DD-HH.MM.SS.NNNNNN The Timestamp is coming from a DB2 database. 得到一个字符串以这种格式进入:YYYY-MM-DD-HH.MM.SS.NNNNNN时间戳来自DB2数据库。 I need to parse it into a java.sql.Timestamp and NOT lose any precison. 我需要将其解析为java.sql.Timestamp并且不会丢失任何精度。 So far I've been unable to find existing code to parse that far out to microseconds. 到目前为止,我一直无法找到现有的代码来解析远远超过微秒。 SimpleDateFormat returns a Date and only parses to milliseconds. SimpleDateFormat返回Date并仅解析为毫秒。 Looked at JodaTime briefly and didn't see that would work either. 简短地看着JodaTime,并没有看到那也行。

Have you tried using Timestamp.valueOf(String) ? 您是否尝试过使用Timestamp.valueOf(String) It looks like it should do almost exactly what you want - you just need to change the separator between your date and time to a space, and the ones between hours and minutes, and minutes and hours, to colons: 它看起来应该几乎完全符合您的要求 - 您只需要将日期和时间之间的分隔符更改为空格,将小时和分钟之间的分隔符更改为分钟和小时和小时:

import java.sql.*;

public class Test {
    public static void main(String[] args) {
        String text = "2011-10-02 18:48:05.123456";
        Timestamp ts = Timestamp.valueOf(text);
        System.out.println(ts.getNanos());
    }
}

Assuming you've already validated the string length, this will convert to the right format: 假设您已经验证了字符串长度,这将转换为正确的格式:

static String convertSeparators(String input) {
    char[] chars = input.toCharArray();
    chars[10] = ' ';
    chars[13] = ':';
    chars[16] = ':';
    return new String(chars);
}

Alternatively, parse down to milliseconds by taking a substring and using Joda Time or SimpleDateFormat (I vastly prefer Joda Time, but your mileage may vary). 或者,通过使用子字符串并使用Joda Time或SimpleDateFormat解析为毫秒(我非常喜欢Joda Time,但您的里程可能会有所不同)。 Then take the remainder of the string as another string and parse it with Integer.parseInt . 然后将字符串的其余部分作为另一个字符串,并使用Integer.parseInt解析。 You can then combine the values pretty easily: 然后,您可以非常轻松地组合这些值:

Date date = parseDateFromFirstPart();
int micros = parseJustLastThreeDigits();

Timestamp ts = new Timestamp(date.getTime());
ts.setNanos(ts.getNanos() + micros * 1000);

You could use Timestamp.valueOf(String) . 您可以使用Timestamp.valueOf(String) The documentation states that it understands timestamps in the format yyyy-mm-dd hh:mm:ss[.f...] , so you might need to change the field separators in your incoming string. 文档说明它理解yyyy-mm-dd hh:mm:ss[.f...]格式的时间戳,因此您可能需要更改传入字符串中的字段分隔符。

Then again, if you're going to do that then you could just parse it yourself and use the setNanos method to store the microseconds. 然后,如果您要这样做,那么您可以自己解析它并使用setNanos方法来存储微秒。

Here's the intended way to convert a String to a Date: 以下是将String转换为Date的预期方法:

String timestamp = "2011-10-02-18.48.05.123";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd-kk.mm.ss.SSS");
Date parsedDate = df.parse(timestamp);

Admittedly, it only has millisecond resolution, but in all services slower than Twitter, that's all you'll need, especially since most machines don't even track down to the actual nanoseconds. 不可否认,它只有毫秒级的分辨率,但在所有服务中比Twitter慢,这就是你所需要的,特别是因为大多数机器甚至没有追踪到实际的纳秒级。

If you get time as string in format such as 1441963946053 you simply could do something as following: 如果您以1441963946053等格式获得字符串时间,您可以执行以下操作:

//String timestamp;
Long miliseconds = Long.valueOf(timestamp);
Timestamp ti = new Timestamp(miliseconds);

I believe you need to do this: 我相信你需要这样做:

  1. Convert everythingButNano using SimpleDateFormat or the like to everythingDate. 使用SimpleDateFormat等将everythingButNano转换为everythingDate。
  2. Convert nano using Long.valueof(nano) 使用Long.valueof(nano)转换nano
  3. Convert everythingDate to a Timestamp with new Timestamp(everythingDate.getTime()) 将everythingDate转换为具有新时间戳的时间戳(everythingDate.getTime())
  4. Set the Timestamp nanos with Timestamp.setNano() 使用Timestamp.setNano()设置Timestamp nanos

Option 2 Convert to the date format pointed out in Jon Skeet's answer and use that. 选项2转换为Jon Skeet答案中指出的日期格式并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM