The month, and just the month, is zero-based. Add 1 for display.
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView dateDefault = (TextView)findViewById(R.id.dateDefault);
TextView timeDefault = (TextView)findViewById(R.id.timeDefault);
DatePicker dp = (DatePicker)this.findViewById(R.id.datePicker);
dateDefault.setText("Date defaulted to " + (dp.getMonth() + 1) + "/" +
dp.getDayOfMonth() + "/" + dp.getYear());
dp.init(2008, 11, 10, null);
TimePicker tp = (TimePicker)this.findViewById(R.id.timePicker);
java.util.Formatter timeF = new java.util.Formatter();
timeF.format("Time defaulted to %d:%02d", tp.getCurrentHour(),
tp.getCurrentMinute());
timeDefault.setText(timeF.toString());
tp.setIs24HourView(true);
tp.setCurrentHour(new Integer(10));
tp.setCurrentMinute(new Integer(10));
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@+id/dateDefault"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<DatePicker android:id="@+id/datePicker"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/timeDefault"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TimePicker android:id="@+id/timePicker"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<DigitalClock
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<AnalogClock
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
Related examples in the same category
1. | Write Date to AudioTrack | | |
2. | Represents a date using an integer, in a similar fashion to the implementation in Microsoft Excel. | | |
3. | A formatter that formats dates to show the elapsed time relative to some base date. | | |
4. | Iso Date | | |
5. | Get the string of the date using format "yyyy-MM-dd HH:mm:ss" | | |
6. | Parse a string that contains date, return a date object using format "yyyy-MM-dd HH:mm:ss" | | |
7. | Create Date from timestamp | | |
8. | Convert date in RFC2822 and UTC strings, and to build Date from string of dates in RFC2822 and UTC format | | |
9. | Gets a "HH:mm:ss.SSS EEE dd MMM yyyy" representation of a Date | | |
10. | Gets a "yyyy MMM dd, HH:mm:ss.SSS" representation of a Date | | |
11. | Gets a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" representation of a Date | | |
12. | Parses a String for a "EEE, dd MMM yyyy hh:mm:ss 'GMT'" formatted Date | | |
13. | Formatting and parsing the various date formats we expect to encounter. | | |
14. | ISO8601, ISO8601, RFC822 Date format | | |
15. | Easter Date | | |
16. | Parse Date | | |
17. | compare Two dates with Day value | | |
18. | compare Two dates with Second value | | |
19. | Get start of a date | | |
20. | Get start date of a Month | | |
21. | Get start date of a year | | |
22. | Create date from year, month and day value | | |
23. | Create Date from year, month, day, hour, minute, second | | |
24. | Get year value from Date | | |
25. | Get Month value from Date | | |
26. | Get Day value from Date | | |
27. | Get now in Date and Millis-seconds | | |
28. | Add day, Month and year to a Date | | |
29. | Parse date in format of yyyyMMddHHmmss or yyyyMMdd | | |
30. | String to Date | | |
31. | Convert date value in long to YYYYMMDDHHMMSS format | | |
32. | Convert Date to Number | | |
33. | Get short and long date String | | |
34. | Convert Java Date To Xml Time | | |
35. | Convert dates to Julian dates. | | |
36. | Parse an RFC 822 date string. | | |
37. | Format a date into a format suitable for SQLite | | |
38. | Dot String to Date | | |
39. | Parses an RFC822 formatted date string. | | |
40. | Formats a Date according to RFC822. | | |
41. | DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date); | | |
42. | yyyy-MM-dd HH:mm:ss date format | | |
43. | Utility class for formatting and parsing the various date formats we expect to encounter. | | |
44. | Get a string representation of a date as it relates to the current time. | | |
45. | get Date String from milliseconds | | |
46. | Generate a ISO 8601 date | | |
47. | Generate a Calendar from ISO 8601 date | | |
48. | parse Date for list of possible formats | | |
49. | date To String | | |
50. | Get date string for Locale tr | | |
51. | RFC3339 Date | | |
52. | Date formats | | |
53. | build Date Format day-of-week Short | | |
54. | Get end of each day | | |
55. | Get the end of each Month | | |
56. | Get end of a year | | |
57. | calculate Month Distance | | |
58. | calculate Day Distance | | |
59. | Get hour different | | |
60. | format Millis Into Human Readable | | |