How to get date "value" from DatePicker? - android

I don't know how to get the date value from Datepicker.
PickerDialog.java
package com.example.alecksjohanssen.newyorktimes;
/**
* Created by AlecksJohanssen on 3/19/2016.
*/
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import java.util.Calendar;
public class PickerDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{ DateSettings dateSettings = new DateSettings(getActivity());
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayofmonth = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog;
dialog = new DatePickerDialog(getActivity(),dateSettings,year,month,dayofmonth);
return dialog;
}
}
DateSetting.java
package com.example.alecksjohanssen.newyorktimes;
import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;
import android.widget.Toast;
/**
* Created by My name Is Hardline on 2/26/2016.
*/
public class DateSettings implements DatePickerDialog.OnDateSetListener {
Context context;
public DateSettings(Context context) {
this.context = context;
}
#Override
public void onDateSet(DatePicker view, int year, int monthofyear,int dayofmonth) {
Toast.makeText(context, "Selected date" + monthofyear + "/" + dayofmonth + "/" + year, Toast.LENGTH_LONG).show();
}
}
public void setDate() {
PickerDialog pickerDialogs = new PickerDialog();
pickerDialogs.show(getFragmentManager(), "date_picker");
}
It's basically it, it's only shows that Selected date is . Still I'm looking for how to extract the date data so that I can put it on my Begin Date :P.

You can try like this
private void datePicker(){
Calendar calendar = Calendar.getInstance();
final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
DatePickerDialog datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
System.out.print(dateFormatter.format(newDate.getTime()));
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}

Say you have Calendar mCalendar that you want to init the date picker with it.
dialog.getDatePicker().init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int i, int i2, int i3) {
mCalendar.set(i, i2, i3);
}
});
Then to get the picked date
mCalendar.getTime()

Related

How can I get time and date which is selected by user in TimePicker and DatePicker?

package com.example.timepickerdialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showDatePickerDialog(View v) {
// Get Current Date or use your own Date
final Calendar c = Calendar.getInstance();
final int mYear = c.get(Calendar.YEAR);
final int mMonth = c.get(Calendar.MONTH);
final int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog saadadatePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
//get date here
Toast.makeText(MainActivity.this, mYear+"/"+mDay+"/"+mMonth, Toast.LENGTH_SHORT).show();
}
}, mYear, mMonth, mDay);
saadadatePickerDialog.show();
}
public void showTimePickerDialog(View v) {
// Get Current Time or use your own Time
final Calendar c = Calendar.getInstance();
final int mHour = c.get(Calendar.HOUR_OF_DAY);
final int mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog saadatimePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
I want to print that time and date in toast which is selected by user. But in this code only current Date and Time show.
Toast.makeText(MainActivity.this, mHour+":"+mMinute, Toast.LENGTH_SHORT).show();
}
}, mHour, mMinute, false);
saadatimePickerDialog.show();
}
}
Please set below code for datepicker dialog.
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Toast.makeText(mActivity,dayOfMonth + "-" + (monthOfYear + 1) + "-" + year,Toast.LENGTH_LONG).show();
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
For Timepicker dialog
Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
Toast.makeText(mActivity, hourOfDay + ":" + minute, Toast.LENGTH_LONG).show();
}
}, mHour, mMinute, false);
timePickerDialog.show();
Hope it may help you.

How do I subtract two date pickers in Android?

I have two date pickers in my app (fromDate and toDate). When the user selects and sets both dates from these two date pickers, I want my app to automatically show the number of days between them in another edit text (days) box.
How can I do it?
First add OnDateChangedListener for both of the date pickers as
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
fromDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
toDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
Create a function UpdateText to update the EditText days
private void UpdateText() {
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
localCalendar.set(fromDate.getYear(), fromDate.getMonth(), fromDate.getDayOfMonth());
int day1 = localCalendar.get(Calendar.DAY_OF_YEAR);
localCalendar.set(toDate.getYear(), toDate.getMonth(), toDate.getDayOfMonth());
int day2 = localCalendar.get(Calendar.DAY_OF_YEAR);
int day = day2-day1;
days.setText(""+day);
}
Note: Make sure you are imported the right files
import java.util.Calendar;
import java.util.TimeZone;
Complete program
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity {
DatePicker fromDate, toDate;
EditText days;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fromDate = (DatePicker) findViewById(R.id.datePicker1);
toDate = (DatePicker) findViewById(R.id.datePicker2);
days = (EditText)findViewById(R.id.Days);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
fromDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
toDate.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
UpdateText();
}
});
}
private void UpdateText() {
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault());
localCalendar.set(fromDate.getYear(), fromDate.getMonth(), fromDate.getDayOfMonth());
int day1 = localCalendar.get(Calendar.DAY_OF_YEAR);
localCalendar.set(toDate.getYear(), toDate.getMonth(), toDate.getDayOfMonth());
int day2 = localCalendar.get(Calendar.DAY_OF_YEAR);
int day = day2-day1;
days.setText(""+day);
}
}

DateTimePicker run different depends on api level in Android

I want to use Date Time picker in my project .
http://www.parallelcodes.com/android-datetime-picker-example/
This tutorial works perfectly. When I run this datetimepicker in api 19 showing like this web site and I want to do this.
When I run api 22 datetimepicker appear like this.
So, Any suggestions?
I used this code to solve this issue,
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private Calendar calendar;
private Calendar maxCalendar;
private Calendar minCalendar;
private DatePickerDialog.OnDateSetListener onDateSetListener;
private static final String TAG = DatePickerFragment.class.getSimpleName();
public void setup(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener){
this.calendar = calendar;
this.onDateSetListener = onDateSetListener;
}
public void setup(Calendar calendar, Calendar maxCalendar, DatePickerDialog.OnDateSetListener onDateSetListener){
this.calendar = calendar;
this.maxCalendar = maxCalendar;
this.onDateSetListener = onDateSetListener;
}
public void setup(Calendar calendar, Calendar maxCalendar, Calendar minCalendar){
this.calendar = calendar;
this.maxCalendar = maxCalendar;
this.minCalendar = minCalendar;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),android.R.style.Theme_Holo_Light_Dialog_MinWidth, this, year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
if(minCalendar!=null){
dialog.getDatePicker().setMinDate(minCalendar.getTimeInMillis());
}
if(maxCalendar != null){
dialog.getDatePicker().setMaxDate(maxCalendar.getTimeInMillis());
}
return dialog;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
if(onDateSetListener != null){
onDateSetListener.onDateSet(view, year, month, day);
}else{
Log.w(TAG, "onDateSetListener callback is not initialized.");
}
}
}
and call like this ,
public void showDatePicker() {
Calendar calendar = Calendar.getInstance();
DatePickerFragment fragment = new DatePickerFragment();
fragment.setup(calendar, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
}
});
fragment.show(getSupportFragmentManager(), null);
}
This is android own calendar which design changes for different API. If you want to use same calendar for all API version then you should try with a custom calendar rather then default calendar
If still have any confusion then please let me know

How to disable future date with Datepicker

I am trying to disable date picker for future date but getting not idea...
Here is the what I have tried:
bday.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker =new DatePickerDialog(CreateAccountActivity.this, new OnDateSetListener()
{
#Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
year = selectedyear;
month = selectedmonth;
day = selectedday;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
String currenttime = new String(dateFormat.format(cal.getTime()));
String selectedtime = new String (new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
String futuretime = new String (dateFormat.format(System.currentTimeMillis()));
if(selectedtime==currenttime)
{
Toast.makeText(getApplicationContext(), "you were not born in the future", Toast.LENGTH_SHORT).show();
bday.setText("");
}
else
{
bday.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
}}
},year, month, day);
mDatePicker.setTitle("Please select date");
mDatePicker.show();
}
});
Stackoverflow suggested to use:
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
but in above condition how should I use this.?
I am trying to disable future date. But not getting any idea :(
After Declaring Listener on DatePicker use the mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
package com.keshav.datePicker_With_Hide_Future_Past_Date;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText ed_date;
int year;
int month;
int day;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_date=(EditText) findViewById(R.id.et_date);
ed_date.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
final DatePickerDialog mDatePicker =new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener()
{
#Override
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday)
{
ed_date.setText(new StringBuilder().append(year).append("-").append(month+1).append("-").append(day));
int month_k=selectedmonth+1;
}
},year, month, day);
mDatePicker.setTitle("Please select date");
// TODO Hide Future Date Here
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// TODO Hide Past Date Here
// mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis());
mDatePicker.show();
}
});
}
}

How to use DatePicker widget in android?

enter code hereI have used the DatePicker widget in android.Upon clicking that I want the datePickerdialog to show up and date should be set and the same I want to set to a textview.
Thanks in advance.
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
#Override
// TODO Auto-generated method stub
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
startDateTextView.setVisibility(View.VISIBLE);
startDateTextView.setText(dayOfMonth + "-"+ (monthOfYear + 1) + "-" + year);
}
}, year, month, day);
dpd.show();
the above code i used,bt this works before i click on datepicker. please let me know how to use OnClickListener of datepicker to this
Try below code.
package com.example.datetimepicker;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import java.text.DateFormat;
import java.util.Calendar;
public class MainActivity extends Activity {
Button date,time;
TextView preview;
int year,monthofyear,dayofmonth,hourofday,minute;
Calendar cal = Calendar.getInstance();
DateFormat dt = DateFormat.getInstance();
TimePickerDialog.OnTimeSetListener tpd;
DatePickerDialog.OnDateSetListener dpd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date = (Button)findViewById(R.id.date);
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new DatePickerDialog(MainActivity.this,dpd,
cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)).show();
}
});
time = (Button)findViewById(R.id.time);
time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new TimePickerDialog(MainActivity.this,tpd,
cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),
false).show();
}
});
preview = (TextView)findViewById(R.id.textView1);
dpd = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
preview.setText(new StringBuilder().append(year+"/")
.append(monthOfYear+"/").append(dayOfMonth));
}
};
tpd = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker arg0, int hourofday, int minute) {
// TODO Auto-generated method stub
cal.set(Calendar.HOUR_OF_DAY, hourofday);
cal.set(Calendar.MINUTE, minute);
preview.setText(new StringBuilder().append(hourofday+"/")
.append(minute));
}
};
}
}
you can use like this:
//variables
DatePicker dpResult;
static final int DATE_DIALOG_ID = 999;
//methods
public void setCurrentDateOnView() {
dpResult = (DatePicker) findViewById(R.id.dpResult);
dpResult.setVisibility(View.GONE );
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
dpResult.init(year-7, month, day, null);
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
mbirthdayView.setText( (day)+(".")+(month + 1)+(".")+(year));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
and finally you can call datepicker like this:
showDialog(DATE_DIALOG_ID);

Categories

Resources