I want to make stopwatch with current time. I have two button id is btn1
and btn2
. I want that when I click on button one it should start calculation time from current device time and when I click on stop it should stop with calculating difference between start and stop time. For example let current time is 10.00 my stop watch calculation time from 10.00 and when I stop it the time is 10 .15 . Then it should show the time is 15 minute of work with current start and stop time. I am using Android Studio with Java language. Please any body help me
CodePudding user response:
I have made a repository on github for this. You can refer here for the repository. Here, it will use these services
- Shared Preferences (In class
PrefUtils
) - Background Service (To run the app in background, In class
TimerService
) - Good UI/UX
The best thing about this app is
- It runs the stop watch even in the background
- Needs min api level of 21
- It has action buttons on the notification to
Stop
,Start
andReset
the stop watch.
If you don't know how to open GitHub project in Android Studio, you can refer here to open it on Android Studio.
CodePudding user response:
make stop watch with current time
Instant.now().toString()
Or:
ZonedDateTime.now().format( DateTimeFormatter.ofLocalizedDateTime( FormatStyle.LONG ) )
start calculation time from current device time
Instant start = Instant.now() ;
when i click on stop ut should stop
Instant end = Instant.now() ;
calculating difference between start and stop time.
Duration elapsed = Duration.between( start , end ) ;
show the time is 15 minute of work
long minutesElapsed = elapsed.toMinutes() ;
with current start and stop time.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ) ;
String output =
start.atZone( ZoneId.systemDefault() ).format( f )
" to "
end.atZone( ZoneId.systemDefault() ).format( f )
" is "
minutesElapsed
" minutes."
;