1

Problematic code:

public static final String DATEFORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

public static String getAsString(Object dateStr)
{

    if ( dateStr== null || dateStr.toString().equalsIgnoreCase("null"))
    {
        return null;
    }
    // here I am getting exception
    return (new SimpleDateFormat(DATEFORMAT)).format((Timestamp)dateStr);  
}

Can you please help me to avoid the class cast exception?

3
  • 4
    Well presumably dateStr is a string, given the error message. Why are you accepting an Object reference if you actually require it to be a Timestamp? The way to avoid the exception is to not cast things inappropraitely... but we have no idea what's in the string, so we can't help you convert that into a Timestamp... Commented Sep 28, 2016 at 5:49
  • refer this link for answer stackoverflow.com/questions/25892417/… Commented Sep 28, 2016 at 5:52
  • The value of dateStr is 2016-09-28T00:00:00Z Commented Sep 28, 2016 at 10:48

2 Answers 2

3

Instead of casting the String to a Timestamp, you can use the function Timestamp.valueOf(String).

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

1 Comment

public static final String DATEFORMAT = "yyyy-MM-dd 'T'HH:mm:ss'Z'"; public static String getAsString(Object dateStr) { if ( dateStr== null || dateStr.toString().equalsIgnoreCase("null")) { return null; } // here I am getting exception return (new SimpleDateFormat(DATEFORMAT)).format(Timestamp.valueOf(dateStr)); } passing value as 2016-09-28 T00:00:00 Z
0

You just can't do this because you have no idea what's in the string. Maybe the datestr in your code is like this "abcdredsad". If your are sure that this datestr object is a date string,and you just want to change the formatter from "yyyyMMdd HH:mm:ss" to "yyyy-MM-dd HH:mm:ss", try this.

public static void main(String[] args) {
    String date = StringToDate("20160928 15:00:00");
    System.out.println(date);
}

public static String StringToDate(Object datestr) {
    Date d = new Date();// note: set a default value here 
    Date date;      
    if (datestr == null)
        return null;    
    // you must know the old pattern of the datestr in your code
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    try {
        d = sdf.parse(datestr.toString());// if your formatter is wrong, exception occurs here.
    } catch (ParseException e) {
        e.printStackTrace();
    }
    date = new Date(d.getTime());// if d didn't have a default value, wrong here.

    sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    return sdf.format(date);
}

1 Comment

I saw the suggestion below,and it remind me of this.It's an Exception: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]

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.