I am creating a "To-do Things" app where a user can create tasks to be done. In the Main activity I have a fragment which consists of three EditTexts ie Notification Content , Date & Time of notification.
Each new entry is added to a database. This database is displayed in form of ListView in another activity.
My Questions are-:
Q1) I want to compare date & time of each entry & print only those which are about to come ie whose date & time is after the present date & time.So how should I perform the comparison?
Q2) When the date & time is reached generate a notification .
Q1) Use the Calendar class. You can take a Calendar object and set its date and time with your user data (using Calendar.set) and then compare with another calendar which you set to the current time as follows:
Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
To compare two calendars you can use the Calendar.before method, or you can convert them back into "timeInMillis" and calculate the difference in milliseconds.
Q2) Use theAlarmManager and a BroadCastReceiver
Related
I am creating application with Calendar View and i want to store data of currently selected date in variable. To do that I used OnDateChangeListener. Everything is working but if I don't select any other date when opening activity and leave Calendar on default date (of course it highlights a date on which I am opening the app), variable is storing nothing and OnDateChangeListener is not working. In short I have to click on any date on calendar to save it in variable. Date which is highlighted by default is not getting stored in variable. Sorry for my bad English.
I created simple DateFormate and converted it into string. Then created 3 strings like year, month and date and then I assigned those 3 strings as substring of simple DateFormat.
..and later retrieve and show them as Strings?
I'm asking the user some input and I want to store both the date (i.e., day, month and year) and the time (i.e., the hour of the day) this input was submitted. Each submission is then saved in my SQlite database, and later retrieved from a RecyclerView.
I'm facing two problems at least. Right now I set up two TEXT fields in the database, FIELD_DATE and FIELD_TIME, where I'd like to store the string representation of date & time, in a format depending on the Android user locale.
From what I've read, the android.text.format.DateFormat should help me. So I set:
java.text.DateFormat dateFormat = DateFormat.getMediumDateFormat(getActivity());
java.text.DateFormat timeFormat = DateFormat.getTimeFormat(getActivity());
Now I think I should call format(Date d) on both objects to get my string, but I don't know where do I get this Date object - don't even know if my two lines are correct. So:
How to get a string representation of current date & time, based on the user defined (at OS level) locale?
That said (asked), I wonder if two fields for date & time are really what I'm looking for. As said, at the end I would like to show a RecyclerView reading the database. In that I will also need to filter out entries based on date, i.e.
Entries referring to last week // last month // All
entries
So I'm also asking:
Is a two-text-fields pattern the right choice to store date & time, given the need to easily filter out entries belonging to, say, last week? Should I better have separate columns for day, month and year?
How to query the database to have only last week rows, given the FIELD_DATE / FIELD_TIME structure (or any other better structure you can suggest)?
I'm quite stuck on these three questions.
Edit: finally came up with how to get the strings I wanted at first, it was as simple as instantiating a new Date object:
Date d = new Date();
String date = DateFormat.getMediumDateFormat(getActivity()).format(d);
String time = DateFormat.getTimeFormat(getActivity()).format(d);
Now I have both the needs to display these strings (which is quite simple, as they are already formatted) and to apply some filter to the db, like entries belonging to last week (which, in turn, would be quite simple with current time in millis since 1970). What to do?
If you want to be able to run complex queries such as find all records from last week, I would recommend storing a timestamp in an integer instead. A timestamp is expressed as the number of milliseconds since the Epoch (Jan 1, 1970). It makes it easy to make queries on exact date and time ranges.
The timestamp is easily found from e.g. System.currentTimeMillis().
The other approach would be to use sqlite's built in date type, but I would personally choose the timestamp approach.
Is there any reason you would want to store it in the current locale's format in the first place? If you are displaying the date to the user you're likely better of formatting the timestamp into a date when displayed, using one of the many date features of Java and android such as java.util.Calendar, java.util.Date, android.text.SimpleDateFormat etc.
As an example, you could run this code to get the timestamp of the start of this month:
Calendar now = Calendar.getInstance();
now.set(Calendar.DAY_OF_MONTH, 1);
now.set(Calendar.HOUR_OF_DAY, 0);
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
long startOfThisMonth = now.getTimeInMillis();
hello my bro & sis i am new in android, currently i am working in android database site. my application user used app from the multiple country, So my problem is how can i insert current time of every user from different locale wise when user perform any database transaction, even if user's android mobile device current time is not proper set yet i want to insert current time in database of his region.
Note: currently i am using MySQL database
Now i am insert current time of user i this way
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).concat(" 00:00:00");
but this is working for only one locale which is default time zone is set. So, Please somebody help me how to resolve my problem.
For saving time inside a database, the best and most country compatible way is to use the time as Long. For example, create a calendar instance:
Calendar cal = Calendar.getInstance();
Long timeToSave = cal.getTimeInMillis();
Then You have to save this Long value inside the database. With this value, You can set all Dateformats for all time zones, also AM/PM values and so on.
I'd recommend to keep the DateTime type in SQL as Long.
Please try this code:
Long now = System.currentTimeMillis();
If you ever need to, you will be able to convert the Long value to string for the current locale:
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.LONG, Session.currentLocale);
String date = dateFormat.format(yourStoredDateAsLong);
or
String date = dateFormat.format(System.currentTimeMillis());
I have a database with a table which contains a start time and an end time. The user has to select both through the Android app, and then I would store the two times in my database.
For now, I have 2 TimePicker in my xml file, and I have 2 TimePicker in my java file TimePicker start_time = (TimePicker) findViewById(R.id.timePickerStart);
I tried to create a Time : Time start = new Time(start_time.getCurrentHour(), start_time.getCurrentMinute(), 0); but this method happens to be deprecated. Do you know another way to do it?
My purpose is to send a notification (Toast?) to the user at those times. Do you know how can I link the current time with the time entered by the user?
I'm sorry for asking so many questions, but I'm really lost here :(
Use Date class instead of Time.
Even better use Calendar. Most methods of Date are deprecated.
Calendar calendar = new GregorianCalendar(0, 0, 0, hour, minute, 0);
I am basically using a datetime control in my application.
Wherein on a click event i am initiating datetime dialog.
In another scenario, i want to move current date, to next day, or previous day.
I don't want the dialogbox of date time control to be displayed.
Is it possible??
The DatePicker and TimePicker controls are available as widgets for you to use in your layouts or code.
You can then build the next day / previous day functionality yourself by setting the date / time of those controls.