I have one activity that i use to create customer, in this activity i have one edit text that can show the current date when user open screen. its can be change to future date but not past date so i have used
d2.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
to set current date as a minimum date and onClick i have opening DatePickerDialog. But, when click on that edit text app is crashed and this error show in Logcat.
java.lang.IllegalArgumentException: fromDate: Wed Aug 22 16:31:24
GMT+05:30 2018 does not precede toDate: Wed Aug 22 00:00:00 GMT+05:30
2018
here is my edittext onClickListner code.
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.dateFormatForDOB);
try {
cal.setTime(sdf.parse(edt.getText().toString()));
} catch (ParseException e) {
e.printStackTrace();
}
DatePickerDialog d2 = new DatePickerDialog(CreateUpdateOpportunityActivity.this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int years,
int monthOfYear, int dayOfMonth) {
year = years;
month = monthOfYear;
day = dayOfMonth;
tv.setText(DateUtil.getDateOfBirth(year + "-" + (month + 1) + "-" + day));
}
}, year, month, day);
d2.getDatePicker().setSpinnersShown(true);
d2.getDatePicker().setCalendarViewShown(false);
d2.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
d2.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d2.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
d2.show();
d2.setCanceledOnTouchOutside(false);
i used other solution like
d2.getDatePicker().setMinDate(System.currentTimeMillis());
or
d2.getDatePicker().setMinDate(new Date().getTime() - 10000);
but still app crash and same error comes
if i comment above line of code, my app run without crash.
you need to check if calendar date(cal.getTimeInMillis()) is greeter then your current date(System.currentTimeMillis())
like this
if (cal.getTimeInMillis() < System.currentTimeMillis()) {
// Toast.makeText(getApplicationContext(),"Current time is big",Toast.LENGTH_SHORT).show();
d2.getDatePicker().setMinDate(cal.getTimeInMillis());
} else {
// Toast.makeText(getApplicationContext(),"Current time is small",Toast.LENGTH_SHORT).show();
d2.getDatePicker().setMinDate(System.currentTimeMillis());
}
may be this help
Related
I have a textview and when I click on it, datepicker dialog opens up. I am trying to display today's date on the datepicker dialog. If today is October 11, 2021, the datepicker dialog shows Novermber 11, 2021. But when I select the dates, the textview shows the correct date which is October 11, 2021. The problem is the month which is for some reason up by one.
public View.OnClickListener openDatePicker = new View.OnClickListener() {
#Override
public void onClick(View view) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd", Locale.ENGLISH);
String todaysDate = LocalDate.now().toString();
Log.i("todays date", todaysDate);
LocalDate localDate = LocalDate.parse(todaysDate, formatter);
openDatePickerForParty(localDate);
}
};
public void openDatePickerForParty(LocalDate givenLocalDT){
DatePickerDialog startDateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
AddNewEventScreen.this.textViewEventDate.setText(year + "-"+ (month) + "-" + day);
}
}, givenLocalDT.getYear(), givenLocalDT.getMonthValue(), givenLocalDT.getDayOfMonth());
Log.i("thismonth", String.valueOf(givenLocalDT.getMonthValue()));
startDateDialog.show();
}
The Log "thismonth" also displays the correct month of 10.
The reason for confusion here is the fact that DatePickerDialog widget accepts month value from 0 to 11.
monthOfYear -----> int: the initially selected month of the year (0-11 for compatibility with Calendar#MONTH)
So when you pass 10 for October for it its November. Just subtract the month value by 1 before initializing the DatePickerDailog and then its all fine. More info here
I have a DatePicker and a TimePicker. I am using both of them together to set a date in epoch format to my backend. But when I convert the calculated epoch its completely off.
In my onCreate
whenToTravel = today = Calendar.getInstance();
travelDate = (LinearLayout) findViewById(R.id.travelDate);
travelDate.setOnClickListener(this);
datePicker = new DatePickerDialog(this, this, today.get(Calendar
.YEAR), today.get(Calendar.MONTH), today.get(Calendar
.DAY_OF_MONTH));
selectedDate = (TextView) findViewById(R.id.selectedDate);
travelTime = (LinearLayout) findViewById(R.id.travelTime);
travelTime.setOnClickListener(this);
timePicker = new TimePickerDialog(this, this, today.get(Calendar
.HOUR_OF_DAY), today.get(Calendar.MINUTE), false);
selectedTime = (TextView) findViewById(R.id.selectedTime);
In my onDateSet and onTimeSet
#Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear,
int dayOfMonth) {
whenToTravel.set(Calendar.YEAR, year);
whenToTravel.set(Calendar.MONTH, monthOfYear);
whenToTravel.set(Calendar.DAY_OF_MONTH, dayOfMonth);
selectedDate.setText(DATE_FORMAT.format(whenToTravel.getTime()));
//monthOfYear int: The month that was set (0-11) for compatibility
//with Calendar.
}
#Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
whenToTravel.set(Calendar.HOUR_OF_DAY, hour);
whenToTravel.set(Calendar.MINUTE, minute);
selectedTime.setText(TIME_FORMAT.format(whenToTravel.getTime()));
}
Now when I set 31-May-2016 10:01 AM I get 1464710433243 as the epoch. On using http://www.epochconverter.com/. I get
Assuming that this timestamp is in milliseconds:
GMT: Tue, 31 May 2016 16:00:33.243 GMT
Your time zone: 31/05/2016, 21:30:33 GMT+5:30
I was assuming that my time zone will return 31/05/2016, 10:01:33 GMT+5:30
What am I missing here ?
I think got this working, I made this one change and the epoch now reads
Assuming that this timestamp is in milliseconds:
GMT: Wed, 01 Jun 2016 04:31:23.256 GMT
Your time zone: 01/06/2016, 10:01:23 GMT+5:30
whenToTravel = today = Calendar.getInstance() ;
today.setTime(new Date(System.currentTimeMillis())) ;
I have two textview, I am using separate datepicker on textview's onclick listener,I am trying to get difference between selected dates,but I am getting following error
java.text.ParseException: Unparseable date: "26/4/2016" (at offset 0)
on this line
Date past = format.parse(enddt);
Following is my code can any one help me with that
startdates=(TextView)findViewById(R.id.activity_entry_startdate);
startdates.setOnClickListener(this);
enddates=(TextView)findViewById(R.id.activity_entry_enddate);
enddates.setOnClickListener(this);
Like this way i am opening datepickers
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
startc = Calendar.getInstance();
int year = startc.get(Calendar.YEAR);
int month = startc.get(Calendar.MONTH);
int day = startc.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
//dialog.getDatePicker().setMaxDate(new Date().getTime());
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
return dialog;
//return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
startdates.setText(String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year));
Log.d("month", String.valueOf(year));
}
}
public static class DatePickerFragments extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
endc = Calendar.getInstance();
int year = endc.get(Calendar.YEAR);
int month = endc.get(Calendar.MONTH);
int day = endc.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
dialog.getDatePicker().setMaxDate(System.currentTimeMillis());
return dialog;
//return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
enddates.setText(String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year));
Log.d("month", String.valueOf(year));
difs();
}
}
public static void difs()
{
try {
SimpleDateFormat format = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
Locale.ENGLISH);
String enddt= String.valueOf(format.parse(enddates.getText().toString()));
Date past = format.parse(enddt);
Date now = new Date();
System.out.println(TimeUnit.MILLISECONDS.toMillis(now.getTime() - past.getTime()) + " milliseconds ago");
System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");
}
catch (Exception j){
j.printStackTrace();
}
}
your Dateformat is not correct so change the code like this
DateFormat df = new SimpleDateFormat("MM/dd/yyyy",Locale.ENGLISH);
Date past = df.parse(enddt);
Your format is new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH), but your date is 26/4/2016 which is totally different from your SimpleDateFormat.
Use "dd/MM/yyyy" instead of "EE MMM dd HH:mm:ss z yyyy"
You have to change the SimpleDateFormat string in your difs() method. And be sure to use the right one, as some showed here with MMfor the month is not absolutely correct in your case. If you have a date like 26/4/2016, you just have to use one M:
public static void difs()
{
try {
//change here to dd/M/yyyy for a date like 26/4/2016
SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy",
Locale.ENGLISH);
String enddt= String.valueOf(format.parse(enddates.getText().toString()));
Date past = format.parse(enddt);
Date now = new Date();
System.out.println(TimeUnit.MILLISECONDS.toMillis(now.getTime() - past.getTime()) + " milliseconds ago");
System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");
}
catch (Exception j){
j.printStackTrace();
}
}
EDIT:
Your question and your comments are a little bit confusing and different. For explanation, if you have a SimpleDateFormat string like:
Tue Apr 26 00:00:00 EDT 2016
Your SimpleDateFormat must look like:
SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyyy",Locale.ENGLISH);
That means that you have to give the exact String to parse that matches to the SimpleDateFormat. In other words, an example to pass the correct String would be:
Date past = format.parse("Tue Apr 26 01:20:56 EDT 2016");
And if you want to grab a date string like
26/4/2016
Your SimpleDateFormat must look like:
SimpleDateFormat format = new SimpleDateFormat("dd/M/yyyy",
Locale.ENGLISH);
and the String that should be formated must look like this:
Date past = format.parse("26/4/2016");
And so on...as nobody here can exactly know what String you are passing, I recommend to read the Documentation about SimpleDateFormat. It´s one of the easy described and understandable parts of the API:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
I am converting a timestamp into date and time and setting the result on a textview.
For example 1443884578 is Sat 3 October 2015 18:02
I would like to set the above date and time into an alarm manager.After research i found a code that uses a date time picker.
public void onDateSelectedButtonClick(View v) {
// Get the date from our datepicker
int day = picker.getDayOfMonth();
int month = picker.getMonth();
int year = picker.getYear();
// Create a new calendar set to the date chosen
// we set the time to midnight (i.e. the first minute of that day)
Calendar c = Calendar.getInstance();
c.set(year, month, day);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
// Ask our service to set an alarm for that date, this activity talks to the client that talks to the service
scheduleClient.setAlarmForNotification(c);
// Notify the user what they just did
Toast.makeText(this, "Notification set for: " + day + "/" + (month + 1) + "/" + year, Toast.LENGTH_SHORT).show();
}
However its getting only the date and fires the alarm the minute the date occurs.
PROBLEM: I would like to get the date and time from my textview and skip this date time picker in the format i have. Is this possible?
String input = "Sat October 3 2015 18:02"; // Instead of String input = "Mon Feb 06 2015";
Calendar cal = Calendar.getInstance();
Date date = new Date();
// Changed the format to represent time of day
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss", Locale.ENGLISH);
try {
date = sdf.parse(input);
} catch (ParseException e) {
e.printStackTrace();
}
cal.setTime(date);
//We haven't parsed the seconds from the original date so this will result
//in 18:02:00 - 10seconds.
//For a correct calculation, you could parse the seconds as well
//See SimpleDateFormat above, but you would have to provide the original date
//with seconds as well
cal.add(Calendar.SECOND, -10);
scheduleClient.setAlarmForNotification(cal);
I am trying to update system time and date. Setting time works fine as i have system permissions in my app and application is signatured as system app also setting date works fine for Months Jan upto Nov, but when i select December as month it is setting month to Jan. this all is cause of i am adding 1 to month but in a case i dont add 1 it creates problem for every month. if I select Dec it is Showing Nov, if I am selecting Jan it works fine , When I select Feb it again selects Jan, If I select March it selects Feb. every time it is setting one month less than the selected one.
here is my code
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minutes) {
mHour = hourOfDay;
mMinute = minutes;
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth+1, mDay, mHour, mMinute);
Intent intent = new Intent();
intent.setAction(Action);
intent.putExtra("time", c.getTimeInMillis());
getContext().sendBroadcast(intent);
}
};
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
mYear = arg1;
mMonth = arg2;
mDay = arg3;
mTimePickerDialog.show();
}
};
at the other end
long when = intent.getLongExtra("time", -1);
if (DEBUG)
Log.i(TAG, "Incoming time: " + when);
if (when / 1000 < Integer.MAX_VALUE) {
SystemClock.setCurrentTimeMillis(when);
}
Time time = new Time();
TvManager tvmng = TvManager.getInstance();
TimerManager timerMgr = null;
if(tvmng!=null)
timerMgr = tvmng.getTimerManager();
time.set(when);
try {
timerMgr.setClkTime(time, true);
} catch (TvCommonException e) {
e.printStackTrace();
}
Please correct the code as mentioned below.
Calendar c = Calendar.getInstance();
c.set(mYear, mMonth, mDay, mHour, mMinute);
remove the +1 you added after the month.
When you set the month in calender than it should be between 0 to 11 when you pass month +1 and your month is 11 then it will be 11+1 =12 and calender consider it as Jan not December.
When you use the date selected for the purpose of showing to user then you have to add month+1 so it will show exact month ,
But when you pass it to calender again for the purpose of storing users selection store it as it is without adding +1 if you added it before then make it month-1 and then add to calender object.
It should work as you expected.
Also look at my solution to over come the issue.
to get formatted date to display user
public static String getDate(Calendar calendar){
String formattedDate;
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy");
formattedDate=format.format(calendar.getTime());
return formattedDate;
}
To set date to calender instance after selecting date from date picker use it like below.
DialogFragment newFragment = new DatePickerDialogFragment() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
super.onDateSet(datePicker, year, month, day);
Calender curDateInstance=Calender.getInstance();
curDateInstance.set(Calendar.YEAR, year);
curDateInstance.set(Calendar.MONTH, month);
curDateInstance.set(Calendar.DAY_OF_MONTH, day);
txtPickUpDate.setText(DateUtils.getDate(curDateInstance));
}
};