简体   繁体   中英

Comparing String with Timepicker Android

I have a time value in String format, eg "2:00" , and I want to compare it with the the time I have got from timepicker (24 hour format) in my activity. For example I want to check, if the time from timepicker is less than the String value. In simple words, I want to convert the String to 24 hour format and compare it with the timepicker time. I am unable to find any solution.

TimePicker st = (TimePicker) findViewById(R.id.timePicker);
st.setIs24HourView(true); 
String StartTime = "2:00";

Try this to convert to 24 hour format

SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = parseFormat.parse("10:30 PM");
System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM