简体   繁体   English

如何在Java中将varchar转换为时间?

[英]how to convert a varchar to time in java?

i'm copying data from a csv file to my database (mysql) with java. 我正在使用Java将csv文件中的数据复制到我的数据库(mysql)。 i've a time column where values can be h:mm:ss or - h:mm:ss (the - means that we surpassed a certain time delay). 我有一个时间列,其值可以是h:mm:ss- h:mm:ss-表示我们超过了一定的时间延迟)。

so i was obliged to change the column type to varchar . 所以我不得不将列类型更改为varchar

my problem now is i need to compare the value of records in this column, for example i need to show all records where the value of this column is under 30 min, knowing that the field value surpass the 24h format (can be 56:00:00). 我现在的问题是我需要比较此列中记录的值,例如,我需要显示所有记录,其中该列的值在30分钟以下,知道字段值超过24h格式(可以是56:00 :00)。

thanks for your help 谢谢你的帮助

Rather convert it to seconds and store it as a signed integer. 而是将其转换为秒并存储为有符号整数。 You can't do numerical operations directly on strings/varchars, it would be a high cost of massaging it forth and back to the useable format. 您不能直接在字符串/ varchars上执行数字运算,将其推回并返回可用格式的成本很高。 Then why not just store it directly in that format? 那么,为什么不直接以这种格式存储呢?

Here's a kickoff example how to convert your CSV field to seconds: 这是一个启动示例,如何将CSV字段转换为秒:

public static int toSeconds(String time) throws ParseException {
    SimpleDateFormat positiveTime = new SimpleDateFormat("'['hh:mm:ss']'");
    SimpleDateFormat negativeTime = new SimpleDateFormat("'[-'hh:mm:ss']'");

    if (time.startsWith("[-")) {
        return -1 * (int) negativeTime.parse(time).getTime() / 1000;
    } else {
        return (int) positiveTime.parse(time).getTime() / 1000;
    }
}

Here's how you can use it and finally store it in DB: 最终使用它的方法如下:

String time1 = "[00:00:30]";
String time2 = "[- 00:10:20]";

int time1InSeconds = toSeconds(time1);
int time2InSeconds = toSeconds(time2);

// ...

preparedStatement = connection.prepareStatement("INSERT INTO tbl (col1, col2) VALUES (?, ?)");
preparedStatement.setInt(1, time1InSeconds);
preparedStatement.setInt(2, time2InSeconds);

To select times of over 30 seconds, just do like: 要选择30秒以上的时间,只需执行以下操作:

SELECT col1, col2 FROM tbl WHERE col1 > 30

放下[]并将其移至TIME类型字段,您应该会很好

UPDATE tableName SET timeField = REPLACE(REPLACE(varCharField,'[',''),']','')

Hye

This problem can be solved either setting time data in one column and a boolean flag in the other column of same record differ between '-' and non '-' records which I assume is a minus sign this is in general solution. 可以解决此问题,方法是在同一记录的一列中设置时间数据 ,而在同一记录的另一列中设置布尔标志 ,则'-'和非'-'记录之间的差异(我认为这是一个减号,这在一般解决方案中)。 As you are using mysql where you can set the data directly with '-' sign which shows a negative value ( time datatype) in column and as you said you need to compare them with conditions like 30 mins(00:30:00) etc you can use subtime() or addtime() mysql syntax for comparison purposes. 当您使用mysql时 ,可以直接在列中显示负值( 时间数据类型)的“-”号直接设置数据,如您所说,您需要将它们与30分钟(00:30:00)等条件进行比较您可以使用subtime()addtime() mysql语法进行比较。 Hope you find this answer appropriate. 希望您找到合适的答案。

regards 问候

la_89ondevg la_89ondevg

I'm not an expert Java dev, but I know in php what your asking (if I understand it right) can be achieved with strtotime, so using that as a search, I found this thread. 我不是Java开发专家,但是我知道在php中使用strtotime可以实现您的要求(如果我理解正确的话),因此将其用作搜索,我发现了这个线程。

PHP's strtotime() in Java PHP在Java中的strtotime()

hopefully this helps. 希望这会有所帮助。

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

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