0

Following is the code which I am executing and getting error on line 14 as

Type mismatch: cannot convert from String to TimeLimit

TimeLimit timeLimitValues = timeLimitValues(clientCompanyMarketMapId,
                productCatagory, productSubCatagory, supplierId);

String timeLimitValue = timeLimitValues.getTimeLimitValue();
String timeLimitTC = timeLimitValues.getTimeLimitTC();
timeLimitValues.getBookTravelDateTC();
String isPaymentEnabledFlag = timeLimitValues.getIsPaymentEnbld();
String isExpOnBkDate = timeLimitValues.getIsExponBkDate();
String timeExpiryDate = null;
TimeLimit expiryDate = new TimeLimit();

if (isPaymentEnabledFlag.equals("Y")) {
    if (isExpOnBkDate.equals("Y")) {
        if (timeLimitTC.equals("DAYS")) {
            c.add(Calendar.DATE, Integer.parseInt(timeLimitValue));
            timeExpiryDate = sdf.format(c.getTime());
            expiryDate = timeExpiryDate;
            System.out.println("Expiry Date:   " + timeExpiryDate);

        }

Please help/guide

5
  • Can you provide stack trace? Commented Apr 28, 2015 at 5:06
  • expriyDate is of type TimeLimit and timeExpiryDate is of String hence getting the exception (3rd statement in innermost if statement). What exactly you want to do? Commented Apr 28, 2015 at 5:07
  • converting string timeExpiryDate to TimeLimit expiryDate and then returning/setting it later on. Commented Apr 28, 2015 at 5:11
  • @Surmeet You should have some logic to convert the String timeExpiryDate ot TimeLimit instance expiryDate. Where are you handling that? You are simply assigning String to TimeLimit Commented Apr 28, 2015 at 5:16
  • @user3694267, tried that also, it didnt wrk... Commented Apr 28, 2015 at 5:21

1 Answer 1

2

timeExpiryDate is declared in your code as a String. expiryDate is declared as a TimeLimit.

You have the line

expiryDate = timeExpiryDate;

which will not even compile because you are trying to assign a String reference to a TimeLimit reference.

You need to write some sort of conversion method that takes a String and creates a TimeLimit from it. It can be a utility method somewhere or it could be a TimeLimit(String timeRepresentation) constructor in the TimeLimit class. Then for example you could write

expiryDate = new TimeLimit(timeExpiryDate);

or

expiryDate = SomeClass.convertToTimeLimit(timeExpiryDate);

Java is not like C++ (in case that's what you're used to). Java will not automatically call a "conversion" constructor like C++ does. So even if you have defined a TimeLimit(String) constructor, Java will not auto-call it to do the conversion. You need to explicitly call it.

But in any event, there is no way to do the assignment without you writing some conversion method somewhere and then calling it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help but don't wana change the POJO Class that I have made for TimeLimit..... Need some logic to convert String to TimeLimit Object in the Class that I had posted an example above.
Then write some method in some class that does the conversion and call it.

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.