Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Date in Java is adding some extra minutes

Tags:

java

date

I'm converting a string to Date in Java, but I having a problem It is adding some extra minutes to the result Date.

The String has the following format "yyyy-MM-dd HH:mm:ss.sss" and I have created this function:

public static Date parseISO8601(String date) {
    Date result = null;

    try {

        if (!TextUtils.isEmpty(date)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    ISO8601_DATE_FORMAT);

            result = dateFormat.parse(date);
        }

    }
    catch (Exception ex){
        return null;
    }
    return result;
}

public static final String ISO8601_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.sss";

When I use this function with this string "2015-06-11 20:17:56.873" the result is "Thu Jun 11 20:31:33 CST 2015". I am really new coding with Java, I have read a lot of posts but for me everything seems normal, I don´t know why this is happening.

Some ideas?

like image 200
PoliceEstebi Avatar asked Apr 27 '26 07:04

PoliceEstebi


1 Answers

Try this instead:

yyyy-MM-dd HH:mm:ss.SSS

You are using 's' instead of 'S" for millis.

like image 69
The Coordinator Avatar answered Apr 28 '26 20:04

The Coordinator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!