How to transfer the formatted date string from my DatePickerFragment?
As tyczj pointed out you can use the interface method or you could make datapicker class an inner class of your activity class in which case it should be static and you need a weakreference of the outerclass
Using interface.
Define a interface in DatePickerFragment
. Implement the interface in your activity class and set the date to textview.
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
TheListener listener;
public interface TheListener{
public void returnDate(String date);
}
@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);
listener = (TheListener) getActivity();
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null)
{
listener.returnDate(formattedDate);
}
}
}
MainActivity
public class MainActivity extends Activity implements DatePickerFragment.TheListener{
Button b;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
tv= (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void returnDate(String date) {
// TODO Auto-generated method stub
tv.setText(date);
}
}
Second way not sure if this is the best way. I would recommend the above interface method.
public class MainActivity extends Activity {//implements DatePickerFragment.TheListener{
Button b;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
tv= (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
private WeakReference<MainActivity> mActivity;
@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);
mActivity = new WeakReference<MainActivity>((MainActivity) getActivity());
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
MainActivity target = mActivity.get();
if (target != null) target.tv.setText(formattedDate);
}
}
}
Converting String to datePicker Format
This is how you convert a String
to LocalDate
which is of a specific pattern
:
String dateAsString = "28/12/2002";
DateTimeFormatter customDateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localDate = LocalDate.parse(date, formatter);
And then set the value:
endDateDatePicker.setValue(localDate);
Android format date to MM-DD-YYYY from Datepicker
Check this:
date format change in datepicker and calendar
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
if(month < 10){
month = "0" + month;
}
if(day < 10){
day = "0" + day ;
}
searchText.setText(day + "-" + month + "-" + year);
Using moment.js to convert date to string MM/dd/yyyy
I think you just have incorrect casing in the format string. According to the documentation this should work for you: MM/DD/YYYY
moment.js documentation
jQuery UI DatePicker - Change Date Format
Here's one specific for your code:
var date = $('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' }).val();
More general info available here:
- http://api.jqueryui.com/datepicker/#option-dateFormat
- http://api.jqueryui.com/datepicker/#utility-formatDate
Related Topics
Recyclerview - How to Smooth Scroll to Top of Item on a Certain Position
Android Fragment Lifecycle Over Orientation Changes
Access the Http Response Headers in a Webview
Android ==> Memory Analysing ==> Eclipse Memory Analyzer
Android Hide/Unhide App Icon Programmatically
Programmatically Update Widget from Activity/Service/Receiver
Android Image View Pinch Zooming
How to Detect the Swipe Left or Right in Android
Full Webpage and Disabled Zoom Viewport Meta Tag for All Mobile Browsers
Android Lollipop, Appcompat Actionbar Custom View Doesn't Take Up Whole Screen Width
How to Create a Simple Divider in the New Navigationview
Firebase (Fcm): Open Activity and Pass Data on Notification Click. Android
How to Find Older Versions of Eclipse Adt
Android: Disabling Highlight on Listview Click
Displaying Emoticons in Android
How to Keep Fragment Alive When Using Bottomnavigationview with New Navcontroller