Android Event.START_DATE Format - android

The format for Event.START_DATE for storing events in contact will be same across different devices(HTC, Samsung, LG, Sony Ericsson, etc.,) or will it differ? If it differs, is there any way to determine the format used by those manufacturers?

i used the next format:
private static final DateFormat BIRTHDATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
...
final String dateStr = BIRTHDATE_FORMATTER.format(eventDate);

As per sqlite's datatype link http://www.sqlite.org/datatype3.html. The format of the Event.START_DATE will be in one of the following three format.
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich
on November 24, 4714 B.C. according to
the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates
and times in any of these formats and
freely convert between formats using
the built-in date and time functions.

Related

Android, Right way to programticly format date time as time ago

Example, from 1 min ago
i use api and time response result from Api service as:
{
"date_time":"2016-03-10 03:20:30"
}
Please discuss step by step, And if available how can programticly display it in Arabic format
منذ 15 دقيقة
My Code for date in list view adapter
TextView date_time = (TextView) convertView.findViewById(R.id.date_time_list_home);
date_time.setText(m.dateTime());
First, for any date/time manipulation on Android, I highly recommend using the ThreeTenABP library. This is a back port of the Java 8 java.time.* package, circumventing the notoriously disappointing java.util.Date and java.util.Calendar classes.
To parse your "date_time" using this library, you can use the following code:
// I set the ZoneId to systemDefault, but you should really use the ZoneId of the server
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
.withZone(ZoneId.systemDefault());
Instant instant = formatter.parse(m.dateTime(), Instant.FROM);
Android provides the DateUtils class to display date and time information. This class takes into account system settings such as Locale and 12/24-hour format. Therefore, if the Locale of the device is set to any of the Arabic locales (ar_), the date/time will be displayed in a format suited for it.
String display = DateUtils.getRelativeTimeSpanString(
instant.toEpochMilli(),
Instant.now().toEpochMilli(),
DateUtils.MINUTE_IN_MILLIS);
date_time.setText(display);
The last parameter in getRelativeTimeSpanString is the minimum resolution, so setting DateUtils.MINUTE_IN_MILLIS will not display the difference in seconds.
If you insist on using the Java 7 classes, here is the same code using them:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = format.parse(m.dateTime());
String display = DateUtils.getRelativeTimeSpanString(
date.getTime(),
new Date(),
DateUtils.MINUTE_IN_MILLIS);
date_time.setText(display);
If you need a "transition resolution" greater than a single day (i.e. you want to display a date/time that is further than one day in the past as "… days ago") you can use the DateUtils.getRelativeDateTimeString() method instead:
String display = DateUtils.getRelativeDateTimeString(
mContext,
instant.toEpochMilli(),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
DateUtils.FORMAT_ABBREV_ALL);
Any date that is further back than the transitionResolution (in this case, one week) will be displayed in an appropriate date format, instead of the relative format. The minResolution and transitionResolution can be any long value, DateUtils contains other convenient constants such as MONTH_IN_MILLIS and YEAR_IN_MILLIS.
The last parameter takes an integer for formatting flags. These flags override the default formatting that DateUtils uses for each Locale; view the documentation for more information.

Joda library does not work properly

I'm using Joda library in my android application to calculate the duration between two given dates. Using this application to calculate a person's age.
The following code gives this output : 23 years 11 months and 6 days
DateTime from_readable_dateTime = new DateTime(from_date_dt);
DateTime to_readable_dateTime = new DateTime(to_date_dt);
Period period = new Period(from_readable_dateTime, to_readable_dateTime);
from_date_dt in this case is 1990/01/06 and to_date_dt is 2014/09/15. (date format is yyyy/mm/dd). As I mentioned earlier the output of this piece of code with the given inputs must be 24 years, 7 months and 20 days while I get 23 years 11 months and 6 days. What's the problem? Am I doing something wrong or Joda is faulty?
UPDATE-1
I get 3 numbers as year, month and day from 3 number pickers I make a single string as date(start date variable is named from_date_string and end date is named to_date_string), I convert these two strings to date variables (from_date_dt and to_date_dt):
from_date_dt = null;
to_date_dt = null;
diff_dt = null;
dateFormat = new SimpleDateFormat("yyyy/mm/dd");
try {
from_date_dt = dateFormat.parse(from_date_string);
to_date_dt = dateFormat.parse(to_date_string);
} catch (ParseException e) {
e.printStackTrace();
}
BTW, I'm working with Persian calendar. Since I can't use the default date picker, I'm using number pickers as date pickers.
The standard period type which you implicitly use contains weeks. The class Period has another constructor with 3 arguments where you can specify PeriodType.yearMonthDay() as third argument.
LocalDate d1 = new LocalDate(1990, 1, 6);
LocalDate d2 = new LocalDate(2014, 9, 15);
Period p = new Period(d1, d2, PeriodType.yearMonthDay());
System.out.println(p); // output: P24Y8M9D
To explain the result in fine-granular steps:
[1990-01-06] + 24 years = [2014-01-06]
[2014-01-06] + 8 months = [2014-09-06]
[2014-09-06] + 9 days = [2014-09-15]
Another thing to consider, don't use DateTime if your input is just a plain date format. Do you really want to take into account timezone effects? And why do you use SimpleDateFormat although JodaTime has its own formatters?
UPDATE after updated question of OP:
Now the question has become a lot clearer.
First to note generally, if you use number pickers then your original input for year, month and day-of-month are just integers. In that case I would normally not use a formatter at all, but just pass the numbers to the constructor of LocalDate. This constructor will also verify the input automatically. The complex conversion you try is very error-prone (number to string, then string concatenation to a date string, then parsing it with your default timezone and then passing java.util.Date to DateTime-ctor). It can be completely avoided.
Second to note and most important: You write that you use a persian calendar. Then the reason why you cannot use the default date picker is simply that this default date picker does not support the calendrical rules of persian calendar. Is my speculation right? And here the very bad news for you: Jodas classes do NOT support the persian calendar, too, especially LocalDate or DateTime are only designed for the ISO-8601-standard which is based on the modern proleptic gregorian calendar. Month lengths for example are different in gregorian calendar and in persian calendar, hence a completely different calendar arithmetic is required which is not supported by Joda-Time.
So your strange period results are probably explainable by the fact that you tried to let the user define persian year, month and day. And then you try to parse this input leniently (otherwise Joda-Time would immediately complain about odd day-values for example). But the last step - period calculation - must fail because it is based on ISO-8601 in Joda-Time and not persian calendar rules.
Am I right? Or have I misinterpreted your updated question?
A work-around is difficult. If you really want to have period calculation for a persian calendar then you have to build it from the scratch. As far as I know there is actually no library which supports this feature. A guideline for writing a persian solution can be the algorithm discussed in this SO-post however.
UPDATE indicating a solution:
Meanwhile I have implemented the Persian calendar in Time4A, see also this SO-post. So if you are able to use Time4A and combine the PersianCalendar with the algorithm for a multi-unit-period mentioned above then this will solve your problem. Time4A-v3.15-2016a explicitly supports special Persian calendar units which use different rules than gregorian calendar units.

How can i get max data and minimum data from sqlite database in andorid?

In my project i stored the dates in the dateadded filed like below.
01-07-14 12:00
02-07-14 12:00
25-06-14 13.00
When i fire query for minimum date:
Select dateadded from dgadata order by dateadded asc limit 1
That is return 01-07-14 12:00 and even if i write Select min(dateadded) from dgadata the output is same. But here the minimum date is 26-06-14 13.00
And in the max date i write like
Select dateadded from dgadata order by dateadded desc limit 1
Here the result is 25-06-14 13.00 but here the max date is 02-07-14 12:00
I think my query is fine i don't know why i am getting wrong result.
SQLite doesn't have dedicated datetime types, but does have a few datetime functions. Follow the string representation formats (actually only formats 1-10) understood by those functions (storing the value as a string) and then you can use them, plus lexicographical comparison on the strings will match datetime comparison (as long as you don't try to compare dates to times or datetimes to times, which doesn't make a whole lot of sense anyway).
Depending on which language you use, you can even get automatic conversion. (Which doesn't apply to comparisons in SQL statements like the example, but will make your life easier.)
The reason is because the comparison is performed in the stored string. I think that this may help.
For what I can see, it seems that you have defined your dateadded field as TEXT, you should store a timestamp (integer) value instead of a text value, this way it will be easy to order them.
SQLite does not have a storage class set aside for storing dates and/or times. Instead,
the built-in Date And Time Functions of SQLite are capable of storing dates and times
as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24,
4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely
convert between formats using the built-in date and time functions.`
Another possible solution is to store your dates as YYYMMMDDHHMMSS, this way your order by will work aswell.

convert string to date and store in sqlite in android

I have date like this in string datatype from server 2012-09-25 12:44:50.000. How can i change into date and store in sqlite in android....
I have tried like this...
String ackwardDate="2012-09-25 12:44:50.000";
Calendar calendar = Calendar.getInstance();
String ackwardRipOff = ackwardDate.replace("/Date(", "").replace(")/", "");
Long timeInMillis = Long.valueOf(ackwardRipOff);
calendar.setTimeInMillis(timeInMillis);
values.put(DBCREATIONDATE,calendar.getTime().toGMTString());
but i am getting Invalid Long : "2012-10-30T03:12:53.827"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf.parse("2012-09-25 12:44:50.000");
long millisec = date1.getTime();
System.out.println(""+millisec);
What type of data type you define in your sqlite database for store datetime?. If it is Text type then you need to convert this data value to string.
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
`TEXT` as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
`REAL` as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
`INTEGER` as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

Date and time in android

In my appliaction I have to store current date into the database. How can i get the current date and is there is any specific format to store date in database.
Better would be to store the date/time in long in Database and then fetch the long date/time from Database and specify the required format using SimpleDateFormat.
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in
Greenwich on November 24, 4714 B.C. according to the proleptic
Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01
00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
Now for how to insert date in after it.
Use PreparedStatement#setString() or #setLong() respectively.
Hope this explanation works for you..
first convert the date to be stored to a String object using SimpleDateFormat class
code sample:
Date dateToBeStored = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd"); // this format will help you to convert date to 2012/07/04 format string
String dateString = formatter.format(dateToBeStored); // convert string
now read the date string from DB. you should have some means to get the date back from DB.
String readDateStringFromDB = readDate();
Now parse the read date string to date object by parse method of SimpleDateFormat class
Date dateObj = formatter.parse(readDateStringFromDB); // now you have the Date object back
SimpleDateFormat sdfDateTime = new SimpleDateFormat("dd/MM/yyyy"+ "",Locale.US);
String newtime = sdfDateTime.format(new Date(System.currentTimeMillis()));
You can use the Date class to get the epoch time, which could then be stored in the database as an integer. Alternatively you could convert the epoch time to regular time and store it as a date data type.
https://developer.android.com/reference/java/text/SimpleDateFormat

Categories

Resources