• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Is there any existing algortihm to get current time ?

I also need to add an if conditional that makes some comparisons such as


if ( currenttime < 10:00){
... do something ...;

}

to this code

any hint will assist

Thanks already
 
First you call the Calendar

[HIGH]Calendar c = Calendar.getInstance();[/HIGH]

Then you need to create a SimpleDateFormat to get the time format you want

[HIGH]SimpleDateFormat sdf = new SimpleDateFormat("kkmm");[/HIGH]

The format kkmm returns the time in a 24 hour "style". So if the system time was 9:27, kkmm would return 2127. You could put a " in between the kk and mm, but that would break the ability for the date to be converted to an int, which is how we will check if it's before 10.

So, to check if it's before 10 (assuming PM), you would have this:

[HIGH]Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("kkmm");
int i = Integer.parseInt(sdf.format(c.getTime()));
if (i < 2200) {
// do stuff
}[/HIGH]

Hope this helps!
 
Back
Top Bottom