I'm quite new to Java however I'm building small fitness app and I am stuck on how I would go about resetting the number of steps taken when it hits midnight using Calender object. I'm trying to get the steps reset at 12:00 but save the steps that are there into my shared preference at 23:59. Does anybody know how I would go about doing this?
This is what I have so far in regards to using Calander:
public String getTheDate (){
date = Calendar.getInstance().getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
return simpleDateFormat.format(date);
}
I'm a bit stuck so I would be really grateful for any help.
Thanks
Related
I am writing an app that tracks an exercise routine and multiplies the time or reps for the exercises by 10% every week. As I'm a total beginner to Android Studio and Java, I'm having trouble understanding how to write code that executes every time I launch the app and how to write code that only executes once on the first launch.
Here's my code, I have an Exercises class, and in the MainActivity I wrote
Exercises stretch = new Exercises("Stretch", 360);
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
int day = calendar.get(Calendar.DAY_OF_YEAR); //execute this every day
int compare_day = day;
if(day-compare_day>=7){
warmup.setTime((int) (warmup.getTime()*1.1));
int compare_day = day;
}
How do I write the code that would update the day variable every day, but not the compare_day variable? And is there a better solution, one that would do it automatically, without having to open the app every day?
I am very new to java and I have been assigned a task in which I have to select single date (01-01-2011) or range of dates like (from 01-01-2011 to 22-03-2011)along with time, Time can also be optionally selected.
I was previously a web developer and there are a lot of date or time range picker available there and they are very easy to customize. But in JAVA, everything seems complex.
I have seen some examples on internet so far like LGoodDatePicker BUT I don't have any idea how to implement this.
Any tutorial or straight guideline will help me alot.
Thanks
LGoodDatePicker is great, I am also using it. I would suggest you to stick to, what is widely used to get help.
LGoodDatePicker has demo code in GitHub repo. I used those example codes to figure out how to implement it. You can compare those to the screenshots provided here.
Generally, you can create a component with something like this:
DatePickerSettings datePickerSettings = new DatePickerSettings();
datePickerSettings.setFormatForDatesBeforeCommonEra("dd.MM.yyyy");
datePickerSettings.setFormatForDatesCommonEra("dd.MM.yyyy");
TimePickerSettings timePickerSettings = new TimePickerSettings();
timePickerSettings.use24HourClockFormat();
DatePicker datePicker = new DatePicker(datePickerSettings);
panel.add(datePicker);
datePicker.setDateToToday();
A range is nothing but a start and end date. You could use 2 such components for a start date and an end date. You need to check that start date is before end date.
How to get the time or a date of a user turning off an application?
um
Clash of Clans for example, you can see your clan users' last visiting time or last turning off time of the application.
such as "5 minutes ago", or "1 day, 3 hours ago"
i just want to know the specific method name of this and look for it at google android references.
There is no specific method for this, you can get the systems current time and save it in a shared preference.
To get the time and date use:
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
To save this time in SharedPreference:
SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
editor.putString("NameOfThingToSave", currentDateandTime);
editor.commit();
Beware:
This technique largely depends on your system time, if a user changes the time/date on the device your logic will get messed up. The proper way to do this would be to sync the time with your server time!
Most importantly you should use your activities life-cycle methods to determine when the activity starts (OnCreate/onStart) ann when your activity stops (onStop)
Maybe you can save the current time in your onStop() and when you open the application again display the time saved when your app was closed.
You can store all the informations you want using SharedPreferences
I am developing the android application in which persons can enter leave application.
Here users select the date from the Android Calendar and from this selected Date Epoch value is sent to php page on converting the epoch readable date format i get a difference of 1 Day.
As on php page i calculate the to date for the leave using the number of leave and from date but here i m getting the wrong value as from date on the php differs from the selected date on the android application.
Kindly help..
It turned out to be a timezone issue. The dates get a default timezone upon creation. But it makes a difference between 0 and 1 day if there different timezones involved. Especially if you create a date, transfer the timestamp (as long int) and create another date from it.
For more details see the comments to the question :-)
I stumbled upon this piece of code to get current time in EST with daylight saving. It seems to work fine, when checked on the internet by the time displayed in sites providing current time in EST with daylight saving.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("EST5EDT"));
System.out.println(dateFormat.format(new Date()));
I just want to confirm, has anybody used something like this before or there is a better way to achieve the same. I cannot use Joda library due to constraints.
Thanks in advance.
Yes, that's the best (and indeed the only) way to do this using Java's standard libraries.