1

My code:

Date p_selected = new SimpleDateFormat("HH:mm", Locale.US)
                        .parse(String.valueOf(p_hour[0]) + ":" + String.valueOf(p_min[0]));
Log.d("ttt", "current_time = " + calender.getTime() + "user time = " + p_selected);

This is my log where current time is of 2015 and why the user time 1970?

current_time = Wed Sep 30 11:35:43 GMT+05:30 2015  
user time = Thu Jan 01 11:02:00 GMT+05:30 1970.

Anything I am missing here?

Paritosh
  • 2,097
  • 3
  • 30
  • 42
Asif Sb
  • 785
  • 9
  • 38

2 Answers2

0

"HH:mm" in the SimpleDateFormat only parse the hour and minute, so those two match. If you add "YYYY-MM-DD" (might be dd instead of DD) you should get the date as well. And then you'll have to provide the extended input text too of course.

dutt
  • 7,909
  • 11
  • 52
  • 85
  • what u mean by extended input text here?date(2015/09/30)? – Asif Sb Sep 30 '15 at 06:33
  • You currently only provide text to the parser for hour and minute, you need to provide text to parse year, month and day as well. – dutt Sep 30 '15 at 07:09
0

Referring to the help from @ganapat and @arodriguez, the final code is:

Calendar calender=Calendar.getInstance();
            int yy=calender.get(Calendar.YEAR);
            int mm=calender.get(Calendar.MONTH);
            int dd=calender.get(Calendar.DAY_OF_MONTH);

            String date_str=new StringBuilder().append(yy).append("/").append(mm+1).append("/").append(dd).append(" ").toString();

I am getting the current date and passing it to simple formatter.

Date p_selected=new SimpleDateFormat("yyyy/MM/dd HH:mm",Locale.US).parse(date_str+String.valueOf(p_hour[0])+":"+String.valueOf(p_min[0]));

and now I am able to compare the both times.

halfer
  • 19,824
  • 17
  • 99
  • 186
Asif Sb
  • 785
  • 9
  • 38