I am working on a project in which I need to change the time to book order based on current time. What I need to do is I want to get the current time and have to generate a string list of time and populate them in spinner. eg. My start and end time are 11am to 2 pm and current time is 12.30 pm. So I need to take the current time and generate a list view of times of 30 mins gap till end time. So that will be 1:00,1:30,2:00.
public void doWork() {
runOnUiThread(new Runnable() {
public void run() {
try{
TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime);
Date dt = new Date();
int hours = dt.getHours();
int minutes = dt.getMinutes();
int seconds = dt.getSeconds();
String curTime = hours + ":"+minute + ":"+ seconds;
txtCurrentTime.setText(curTime);
}catch (Exception e) {}
}
});
}
class CountDownRunner implements Runnable{
// @Override
public void run() {
while(!Thread.currentThread().isInterrupted()){
try {
doWork();
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}catch(Exception e){
}
}
}
}
I have got this code from So which i can use to get current hours and min in hh:mm format. How to approach further?
Try this code: