How to convert varchar in time in java?

I am copying data from a csv file to my database (mysql) using java. I have a time column where the values ​​can be h:mm:ssor - h:mm:ss( -means that we have exceeded a certain time delay).

so I had to change the column type to varchar.

Now my problem is to compare the values ​​of the records in this column, for example, I need to show all the records where the value of this column is less than 30 minutes, knowing that the field value exceeds the 24-hour format (maybe 56:00:00).

thanks for the help

+2
source share
4 answers

. /varchars, . ?

, 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;
    }
}

, , :

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);

30 , :

SELECT col1, col2 FROM tbl WHERE col1 > 30
+3

[] TIME,

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

+1

, '-' non '-' , , , , . mysql, "-", () , , , , 30 (00:30:00) .. subtime() addtime() mysql. , .

la_89ondevg

+1
source

I am not a Java dev expert, but I know in php that your request (if I understand correctly) can be achieved with strtotime, so using this as a search, I found this thread.

PHP strtotime () in Java

hope this helps.

0
source

Source: https://habr.com/ru/post/1759883/


All Articles