Learn Date and Time in Android - CodeProject
Learn Date and Time in Android - CodeProject
Working with date and time is one of the common task Android developers perform.
Working with date and time is one of the common task Android developers perform. The framework provides a lot of tools and helper classes to help developers work with date and time in
Android so we will start our tutorial by examining the common terms and concept that you will encounter when you work with Android DateTime.
CodeProject
Now that we have covered the basics of date time in Android, let us see how to put them to good use.
Formatting
The steps to format date and/or time in Android are
Here is an example of getting, formatting and displaying todays current date and time
mTodayDate = (TextView)findViewById(R.id.today_date);
//If you want to show only the date then you will use
//DateFormat dateFormat = DateFormat.getDateInstance();
//Format date
String todayDateTimeString = dateFormat.format(todayDate);
//display Date
mTodayDate.setText(todayDateTimeString);
Of course there are many ways to obtain and display the current date and time such as this
What of if you already have an existing date that you want to format, say the date is coming from a field in the database or from an API call. To format and display the date, you first have to
either obtain or convert the date to longand then you can create a method that accepts a long parameter and the format that you want the date to be displayed, here is an example
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
And with that you can call the method like this
1. String you can simply save your datetime as String (Text in SQLite) if all you want to do with the date going forward is display it. Also you can use DateFormat to parse the saved
String to date value and work with it later.
2. Long SQLite database and most database engines are not suitable for persisting non primitive objects like Java classes and since Date is a Java class you cannot save a field of type
Date to a SQLite database. You can however convert your date to milliseconds and save to database. For example here is how you can create a fictitious product table and save it to
database:
Hide Copy Code
private String createProductTable = "CREATE TABLE ProductTable(" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"productname TEXT NOT NULL," +
"datecreated BIGINT";
and with this SQL statement we can create an object of type Product with a long date field like this:
Hide Copy Code
Product temp = new Product();
temp.productName = "Nice Product";
temp.dateCreated = System.currentTimeMillis();
saveDemo(temp);
3. SQLite Datetype while SQLite does not havea storage class set aside for storing dates and/or times you can use SQLite built in datetime functions to work with date, with our
fictitious product table can now be created like this
Hide Copy Code
private String createProductTable2 = "CREATE TABLE ProductTable(" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"productname TEXT NOT NULL," +
"datecreated DATETIME DEFAULT CURRENT_TIMESTAMP";
Here are some steps that you can use to work with date and time that your users select in the picker dialog.
For example if you have a Fragment called BirthDayFragment and you want to give your users a dialog to select their birthdays. When they select their birthday, you want to save that date in
the database. Here is what you can do:
1. Create a Person class with a property of type long to store the birthday as well as other properties you may need
2. In the BirthdayFragment.java create a member variable of type Calendar that stores the date the user selected from the dialog picker
3. Create a DialogFragment that implements DatePickerDialog and set your BirthdayFragment as the target Fragment
4. When user selects a date from the dialog picker, store the date in the Calendar variable in your Fragment
5. Convert the Calendar variable to long and save
6. Here is a code demo to illustrate these steps
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
I hope that this blog post helps you with working with date and time and Android.
Follow Me
Follow @valokafor
<script>jQuery(window).load(function() { ThriveApp.load_script('twitter'); });</script>
<script>jQuery(window).load(function() { ThriveApp.load_script('google'); });</script>
<script type='IN/MemberProfile' data-format='inline' data-id='https://www.linkedin.com/in/valokafor'></script>
<script>jQuery(window).load(function() { ThriveApp.load_script('linkedin'); });</script>
The post Learn Date and Time in Android appeared first on Val Okafor.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)