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.
Related
I want to get the current date as yyyy-MM-dd from SQLite, I use the following query:
***SELECT date('now')***
But instead of returning the current date, it returns the next day from today.
For example, today (2015-12-01) I run the query and it returns (2015-12-02).
What I did wrong?
Image running query + calendar
sqlite date and time functions use UTC time zone internally. In UTC the date was already 2015-12-02.
If you want to use another timezone, you need to specify it explicitly, e.g.
select date('now','-05:00');
I'd suggest to use UTC millisecond timestamps in your database layer though and have the presentation logic such as date formatting with timezone adjustment in your app code.
Reference: https://www.sqlite.org/lang_datefunc.html
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
..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();
i have a monthdisplayhelper to display the month, i want to select days between two date. i.e.,if i selected 2nd July and 9th July, all the dates(3rd to 8th) in between these dates must be selected or highlighted.
You can use the Calendar class. Initialise a new Calendar object for your initial date, then use [calendar instance].add(Calendar.DAY_OF_YEAR, 1) to increment the date by 1 day, it'll automatically roll to the next month if required.
Calendar reference: http://developer.android.com/reference/java/util/Calendar.html
If you know how many days you will need then you could have a loop basically add days and store in a list
Hey,
I want to update the date to a year before 1970.
the updateDate function wants a date since 1970.
the widget itself allow you to select 1900-2100 (from what i read you can change it also using an xml to describe the control)
any solutions?
if not, what widget do you use to select date (e.g. birth date...)
Thanks.
Just use a negative number when you pass in the years.