Date displaying twice in date picker - java

Current date is displaying twice in my date picker android app I don't
understand why this is happening.Can anyone tell me what is causing this.
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR); // current year
int mMonth = c.get(Calendar.MONTH); // current month
int mDay = c.get(Calendar.DAY_OF_MONTH); // current day
datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
t1.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
This is the image

What you are facing is the manner how the date picker is implemented. Usually, the title date should go away if you use it like this:
datePickerDialog.setTitle("");
this call have to be directly before datePickerDialog.show();

You need to give a "time" for the fragment to be rendered.
Try this:
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + 1000);

Related

How to initialise Android DatePicker by Intent?

I've got a list of elements displayed. When I click on one element, a detail view is opening and displaying name, description etc. Also a date shall be changeable in the detail view.
For now, the date is set to today's date when I click, but the DatePicker does not show up (init method fails). I know I am doing something wrong and that there has to be an intent/an activity to start from my DetailView class, since it's not the MainActivity. But unfortunately, I have no idea how to do it.
This is my code in DetailView.java:
public void onDateChange() {
initDatePicker();
dateButton = findViewById(R.id.itemDateButton);
dateButton.setText(getTodaysDate());
}
private String getTodaysDate() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
month = month + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
return makeDateString(day, month, year);
}
private void initDatePicker() {
DatePickerDialog.OnDateSetListener dataSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
String date = makeDateString(dayOfMonth, month, year);
dateButton.setText(date);
}
};
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 style = AlertDialog.THEME_HOLO_LIGHT;
datePickerDialog = new DatePickerDialog(this, style, dataSetListener, year, month, day);
}
public void openDatePicker(View view) {
datePickerDialog.show();
}
The xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemDateText"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="Date"
app:layout_constraintTop_toBottomOf="#id/itemDescriptionWrapper"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemDateButton"
android:text="Change date"
style="?android:spinnerStyle"
app:layout_constraintTop_toBottomOf="#id/itemDateText"
android:onClick="#{() -> controller.onDateChange()}"
/>
I'm also able to send mails and sms from the DetailView by intent. But i just don't know how to start a DatePicker intent.
Any ideas? I would be very grateful. :)
Best regards,
Jess
To show the DatePicker, All you require is to add .show() method of DatePickerDialog inside your initDatePicker() function.
datePickerDialog = new DatePickerDialog(this, style, dataSetListener, year, month, day);
datePickerDialog.show();

Setting user picked date from DatePicker to Calendar

So I need for user to select date from DatePicker Dialog and then when he selects date I want it to be shown on Calendar. For example, if user selects 01/01/2016 I want that date to be shown as some red marked date.
Here's my code:
displayDatumaDodajPisaniIspit = (TextView)dialog. findViewById(R.id.displayDatumaDodajPisaniIspit);
set = (Button)dialog. findViewById(R.id.set);
set.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(HomeScreen.this, listener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
And the listener
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
displayDatumaDodajPisaniIspit.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
calendarView.setDateSelected();
}
};
I don't have idea how to start with it. Tried with some methods in listener but it didn't work.
You should first convert the chosen date into millis -
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
long millis = calendar.getTimeInMillis();
and then set the date in your CalendarView like this -
calendarView.setDate(millis);
As you mentioned you are using MaterialCalendarView, then I guess you can use -
calendarView.setSelectedDate(CalendarDay.from(year,monthOfYear,dayOfMonth));

How to show timepickerdialog with 12h AM/PM

I'm learning Android so I made an "app" but when the app open the timepickerdialog it shows a 24h clock. I need to show 12h AM/PM.
I read that I need to use the is24HourView, but I dont know how and where. Can you show me where? Here is part of the code.
private void setDateTimeField() {
theHour.setOnClickListener(this);
Calendar newCalendar = Calendar.getInstance();
theHourPickerDialog = new TimePickerDialog(this, new OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar theTime = Calendar.getInstance();
theTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
theTime.set(Calendar.MINUTE, minute);
theHour.setText(timeFormatter.format(theTime.getTime()));
}
}, newCalendar.get(Calendar.HOUR_OF_DAY), newCalendar.get(Calendar.MINUTE),true);
}
I tried to put theHourPickerDialog.is24HourView(false) but it did not work XD
Thanks!
Notice that you should call setIs24HourView(false) before you set time on here.

How to convert datepicker format to dd-MM-yyyy

In my database table I use a date format which inserts dates like 04-04-2015.
Then using a datePicker through variable from_date I choose the same date and that date is like 4-4-2015. If I want to select rows using the date selected
from datepicker I get no rows even though rows are available. How can I change the
datePicker date to 04-04-2015 from 4-4-2015, or insert the date like
4-4-2015 instead of 04-04-2015?
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker arg0, int year, int month, int day) {
dateView.setText(new StringBuilder()
// Month is 0 based so add 1
.append(day).append("-") //day
.append(month+1).append("-")//month
.append(year).append(" "));//year
from_date=dateView.getText().toString();
startActivity(new Intent(datefrom.this, dateto.class));
}
};
Code for insertion:
contentvalues.put(VivzHelper.TX_DATE, new SimpleDateFormat("dd-MM-yyyy").format(new Date()));
Using String.format you can achieve what you need.
int day=5,month=4,year=2015;
String date=String.format("%02d-%02d-%d ", day,month,year);
//output date =05-04-2015
private String getDateTime(int year, int month, int day){
Calendar cal = Calendar.getInstance();
cal.set(year, month, day);
String format = "dd-MM-yyyy";
return new SimpleDateFormat(format).format(cal.getTime());
}
You can achieve this using SimpleDateFormat class. For more information visit http://developer.android.com/reference/java/text/SimpleDateFormat.html.
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker arg0, int year, int month, int day) {
Calendar myCalendar = Calendar.getInstance();
myCalendar.set(year, month, day);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
String dateString = formatter.format(myCalendar.getTime());
dateView.setText(dateString);
from_date=dateString;
startActivity(new Intent(datefrom.this, dateto.class));
}
};

DateFormat produces incorrect output

I've got a couple of Buttons that initially display the current date and the current time, respectively. When clicking in the Button that displays the date, it shows a DatePickerFragment that allows the user to choose a date, and then changes the Button's text to the date selected by the user. The other Button does exactly the same but with a TimePickerFragment.
To initialize the Buttons I use the following code:
protected void onCreate(Bundle savedInstanceState){
...
df = DateFormat.getDateInstance();
tf = DateFormat.getTimeInstance();
initDate = new GregorianCalendar();
...
updateDateButtons();
updateTimeButtons();
}
private void updateTimeButtons() {
tf.setCalendar(initDate);
String text = tf.format(initDate.getTime());
btnIniTime.setText(text.substring(0, text.lastIndexOf(":")));
}
private void updateDateButtons() {
df.setCalendar(initDate);
btnIniDate.setText(df.format(initDate.getTime()));
}
Initially, both buttons behave in an expected manner: btnIniTime shows the current time, and btnIniDate shows the current date.
As I said, when the user clicks the btnIniTime button, it shows a TimePickerFragment that prompts the user to choose a time, and the selected time is correctly displayed in btnIniTime.
The problem starts with btnIniDate, that should do the same, but using a DatePickerFragment instead of a TimePickerFragment. When the user selects a date, the button then displays an incorrect date. For example, if I choose 2013 Aug 30, the displayed date turns to be 2013 Aug. 26. If I choose 2013 Sep 1, it then shows 2013 Sep 29!
The classes and methods that I use to change the date ara arranged in the following way:
public abstract static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
protected TaskActivity activity;
protected Calendar c;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = (TaskActivity) activity;
}
#Override
public abstract Dialog onCreateDialog(Bundle savedInstanceState);
public void onDateSet(DatePicker view, int year, int month, int day) {
if(activity instanceof TaskActivity){
setDateResult(year, month, day);
}
}
protected abstract void setDateResult(int year, int month, int day);
}
public static class InitDatePickerFragment extends DatePickerFragment {
#Override
protected void setDateResult(int year, int month, int day) {
activity.setInitDate(year, month, day);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = activity.getInitDate();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
}
...
public Calendar getInitDate() {
return initDate;
}
public void setInitDate(int year, int month, int day){
Log.d("TaskActivity", "Year: " + year + "; Month: " + month + "; Day: " + day);
initDate.set(Calendar.YEAR, year);
initDate.set(Calendar.MONTH, month);
initDate.set(Calendar.DAY_OF_WEEK, day);
updateDateButtons();
}
When you push the button:
#Override
public void onClick(View v) {
if(v.equals(btnIniDate)){
DialogFragment newFragment = new InitDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "initDatePicker");
}
...
}
By the way, when setting the date, LogCat produces the following output (I've chosen 2013 Aug 30):
Year: 2013; Month: 7; Day:30
The Problem might be
initDate.set(Calendar.DAY_OF_WEEK, day);
in your setInitDate(). This updates only the day of the week. So your date jumps +-6
use
initDate.set(Calendar.DAY_OF_MONTH, day);

Categories

Resources