3

In Android, the date of every event (such as birthdays and anniversaries) is saved in String format, e.g. "2011-12-24".

This is at least the case for my phone. Some other phones may perhaps store these dates in other formats if they have a calendar different from the Gregorian one.

Even for phones with the Gregorian calendar set, there are date Strings with hours and minutes and without. I get such a date String when I query the contacts table for an event:

cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.ST‌​ART_DATE))

This results in (for example):

"2011-12-14" or "2011-12-24T00:00:00Z"

You can see that there are lots of different possible formats. In order to calculate with dates, I need to convert them from String to Date objects, needn't I?

How can I do this?

PS: I know there is a parse function for java date objects:

DateFormat myDateFormat = new SimpleDateFormat("dd/MM/yy");
Date tempDate = myDateFormat.parse("24/12/2011");

But in Android, I don't know which format I'll be confronted with, do I?

2 Answers 2

4

Android works with date objects. Anytime you need to parse a date from string is because that date has been serialized, most likely in a file. In that case, you would know the format that was used. Date object is used in preferences, date object is used almost everywhere I can think of that relates to shared services, activites, etc. This means, there's almost never a need to parse string from date unless, like I said you're working with serialized source, reading it from a web service etc. If you can tell us your scenario, you might get more specific answer.

You can get the currently set date format like this:

DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the nice and helpful answer! I'm querying the contacts table for an event: cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE))
... and this is where I get a date in String form, e.g. "2011-12-24T00:00:00Z". How can I get the date as a Date object? There is no method "cursor.getDate()" ...
You can parse it in this case. Also, this date format will be the same regardless of language set, so you can safely assume that.
Ah, so there's no way to get the date object in this case? :D There must be one way - parsing the string is stupid and can lead to wrong results, can't it? How do I know which formats I'll be confronted with? Which timezone?
You will always get the same format, regardless of the user language selection. So whatever you get in your tests, is what you will always get. Not passing the format to the date parse function is a safe way to work with it.
|
0
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

SimpleDateFormat myDateFormat = new SimpleDateFormat("dd/MM/yy");
        Date tempDate;
        try {
            tempDate = myDateFormat.parse("24/12/2011");
              long milliseconds=tempDate.getTime();
              Toast.makeText(getApplicationContext(), milliseconds+"", 1).show();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.