Choice of time and date for choosing date and time in android

I use the date picker and the timing in my application. I want to set the date and time the page loads. Is it possible? Why so?

+43
android android-datepicker timepicker
Apr 28 '11 at 11:30
source share
5 answers

I solved a similar problem as shown below.

Calendar cal = Calendar.getInstance(); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH); int day = cal.get(Calendar.DAY_OF_MONTH); int hour = cal.get(Calendar.HOUR_OF_DAY); int min = cal.get(Calendar.MINUTE); datePicker.updateDate(year, month, day); timePicker.setCurrentHour(hour); timePicker.setCurrentMinute(min); 
+115
Dec 09 2018-11-11T00:
source share
— -

Using JodaTime

JodaTime is an extremely popular and useful library for processing dates and times in Java. If you are not using it, I highly recommend taking a look at it.

Here is a simple example of how I set DatePicker and TimePicker from a DateTime object, which can be the current date or any date from the past or future (the attribute in this case is called inspected_at ):

 DatePicker datePicker = (DatePicker) findViewById(R.id.inspected_at_date); TimePicker timePicker = (TimePicker) findViewById(R.id.inspected_at_time); DateTime inspected_at = DateTime.now() // Typically pulled from DB. int year = inspected_at.getYear() ; int month = inspected_at.getMonthOfYear() - 1; // Need to subtract 1 here. int day = inspected_at.getDayOfMonth(); int hour = inspected_at.getHourOfDay(); int minutes = inspected_at.getMinuteOfHour(); datePicker.updateDate(year, month, day); // Set date. timePicker.setCurrentHour(hour); // Set time. timePicker.setCurrentMinute(minutes); 

Hope this helps.

In JP

+14
Jan 29 '14 at 1:34
source share

There is an updateDate method for updateDate .

Here is what I use in one of my applications without using a dialog:

 //set datePicker to position String date_saved = project_row.getString( project_row.getColumnIndex("project_startdate")); int day = Integer.parseInt(date_saved.substring(0,2)); int year = Integer.parseInt(date_saved.substring(5,9)); String month = date_saved.substring(2,5); int month_code = getMonthInt(month); project_startdate_data = (DatePicker) findViewById(R.id.project_startdate_data); project_startdate_data.updateDate(year, month_code, day); 

where project_row is my cursor and the date is stored in the project_startdate column as 22MAY2012

and you need it:

 private int getMonthInt(String month) { if (month.equalsIgnoreCase("JAN")) return 0; if (month.equalsIgnoreCase("FEB")) return 1; if (month.equalsIgnoreCase("MAR")) return 2; if (month.equalsIgnoreCase("APR")) return 3; if (month.equalsIgnoreCase("MAY")) return 4; if (month.equalsIgnoreCase("JUN")) return 5; if (month.equalsIgnoreCase("JUL")) return 6; if (month.equalsIgnoreCase("AUG")) return 7; if (month.equalsIgnoreCase("SEP")) return 8; if (month.equalsIgnoreCase("OCT")) return 9; if (month.equalsIgnoreCase("NOV")) return 10; if (month.equalsIgnoreCase("DEC")) return 11; //return -1 if month code not found return -1; } 
+7
Nov 28 '12 at 16:45
source share

Check How to use the date picker . and use the update function in onCreate (); if when loading the page you mean the beginning of activity ... I hope this helps.

+2
Apr 28 '11 at 11:40
source share

Mark it .

Use showDialog(TIME_DIALOG_ID); function in onCreate();

+1
Apr 28 '11 at 12:43 on
source share



All Articles