0

I am trying to convert string in java.util.date but I am having following errors:

 HelloWorld.java:10: error: incompatible types: Date cannot be converted to    String                                                                                                                                                                       
        return FORMATTER.parse(date);                                                                                                                                                                                                               
                              ^                                                                                                                                                                                                                     
 HelloWorld.java:16: error: incompatible types: String cannot be converted to Date                                                                                                                                                                       
    Date date = convertStringToDate("2015-08-03 09:19:00.000");    

My code is below:

import java.text.SimpleDateFormat;
import java.util.Date;

public class HelloWorld{

private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

 public static String convertStringToDate(String date) {
    if(date!=null){
        return FORMATTER.parse(date);
    }
    return null;
}

 public static void main(String []args){
    Date date = convertStringToDate("2015-08-03 09:19:00.000");
    System.out.println(date);
 }
}
1
  • Kindly verify my answer and confirm whether it resolved your issue. Commented Aug 25, 2015 at 0:35

3 Answers 3

5

Change your method signature into this instead:

public static Date convertStringToDate(String date)

SimpleDateFormat.parse returns a Date, not a String.

Also, you need to handle the checked ParseException that the parse method may throw, either by declaring throws ParseException in the signature (and handling the exception in main), or by wrapping the exception into a RuntimeException (effectively terminating the program when bad input is given):

public static Date convertStringToDate(String date) {
    if (date != null) {
        try {
            return FORMATTER.parse(date);
        } catch (ParseException e) {
            // nothing we can do if the input is invalid
            throw new RuntimeException(e);
        }
    }
    return null;
}

Finally, you should notice that you can only parse hour values ranging from 1 to 12 with your current format (yyyy-MM-dd hh:mm:ss). If you'd want to parse according to the 24-hour clock, you should use the HH pattern for the hour part instead:

private static final DateFormat FORMATTER = new SimpleDateFormat(
                                                "yyyy-MM-dd HH:mm:ss");
Sign up to request clarification or add additional context in comments.

Comments

2

Your specified format is not matching with data format.

format "yyyy-MM-dd hh:mm:ss" is NOT compatible with data "2015-08-03 09:19:00.000" because of two reason: (1) AM/PM is missing in date as 'hh' takes 1-12 hrs , and (2) milliseconds present in date string

Replace below mentioned line and your issue will be resolved.

private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

But, none of the above mentioned issue can cause the exception you got as milliseconds is ignored by default.

Possible reasons of exception are return type of method "convertStringToDate" or hh having value beyond the range between01 and 12.

  1. change return type from String to Date
  2. Change date format hh to HH.

2 Comments

The milliseconds in the input string are simply ignored because parse "may not use the entire text of the given string". Actually, even the omission of AM/PM does not matter unless we call FORMATTER.setLenient(false); before parsing.
@MickMnemonic : thanks buddy :) I assumed all possible cases because the code snippet provided in question contains compile time error.
1

It is because you are trying to return a String from your method. Change it to Date.

public static Date convertStringToDate(String date) {
    if(date!=null){
        return FORMATTER.parse(date);
    }
    return null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.