-2
public static Long relativeTime(String dateToParse) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d = null;
    try {
        d = sdf.parse(dateToParse);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    SimpleDateFormat output = new SimpleDateFormat("d MMM yyyy  HH:mm");

        output.setTimeZone(TimeZone.getDefault());


    String formattedTime = output.format(d);
    Date gg = null;
    try {
        gg = output.parse(formattedTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return gg != null ? gg.getTime() : 0;
}

it is giving me error

java.lang.nullpointerexception attempt to invoke virtual method long java.util.date.gettime on a null object reference at java.util.calendar.settimecalendar.java1197 java.text.simpledateformat.formatimplsimpledateformat.java527 java.text.simpledateformat.formatsimpledateformat.java829 java.text.dateformat.formatdateformat.java314 co.helpdesk.faveo.pro.helper.relativetimehelper.java202 co.helpdesk.faveo.pro.frontend.adapters.ticketoverviewadapter.onbindviewholderticketoverviewadapter.java64 co.helpdesk.faveo.pro.frontend.adapters.ticketoverviewadapter.onbindviewholderticketoverviewadapter.java27 android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103

2 Answers2

1

just change this one line in your code:

Instead of

Date d = null;

use

Date d = new Date();

Hope this helps!!!

sumit
  • 1,047
  • 1
  • 10
  • 15
  • Thanks for the suggestion.....it is solved actually the data is coming from the server.....luckily it was server fault....but again thanks.... – sayar samanta Jul 11 '17 at 06:55
0

After the catch statement write this code, and use calender.getTime() to get the time.

Calendar calendar = Calendar.getInstance();
calendar.setTime(gg);

And

return gg != null ? calendar.getTime() : 0;

Hope this solves your problem.

Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26