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())) ;
Related
With a DatePicker I choose a Date and I need the date and the timestamp
from the chosen date:
Calendar calendar = Calendar.getInstance(Locale.GERMANY);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Ctag = sdf.format(calendar.getTime()).substring(0,3)+".";
CdTag = String.format("%02d",dayOfMonth);
Cmonat = String.format("%02d",monthOfYear+1);
Cjahr = Integer.toString(year);
Cdatum = CdTag+"."+Cmonat+"."+Cjahr;
Ctimestamp = (int) (long) calendar.getTimeInMillis();
getDatum.setText(Ctag+","+CdTag+"."+Cmonat+"."+Cjahr);
Snackbar snackbar = Snackbar.make(cl_main,String.valueOf(Long.valueOf(calendar.getTimeInMillis()).intValue()),3000).setDuration(Snackbar.LENGTH_INDEFINITE);
snackbar.show();
}
};
So I set the date which is picked and calendar.getTimeInMillis() I want the timestamp from the date.
But for example I choose today 18.04.2019 it returns 843974600
and when I convert it on a UNIX convert website it gives me
following date:
GMT: Sunday, 29. September 1996 05:23:20
Your time zone: Sonntag, 29. September 1996 07:23:20 GMT+02:00 DST
Relative: 23 years ago
You can try with this code, it should work :
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
Long timeinmilli = calendar.getTimeInMillis(); //will give tou time in ms
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //format of the date, choose what you want
Date resultdate = new Date(timeinmilli); //create an object date associated to the time in ms
String date = DateFormat.format(resultdate).toString(); //convert the date into a string with the format you choose
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
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));
}
};
how to block only past date in DatePicker in android?
Am using sample code in that past date and current date getting blocked ,
I need only past date get blocked not current date here is my code
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int yearSelected,
int monthOfYear, int dayOfMonth) {
year = yearSelected;
month = monthOfYear;
day = dayOfMonth;
int n_mon = monthOfYear+1;
business_date_et.setText(day+"-"+n_mon+"-"+year);
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
business_time_et.setText(hour+":"+minute);
}
};
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog da = new DatePickerDialog(this, mDateSetListener,
mYear, mMonth, mDay);
Calendar c = Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date newDate = c.getTime();
da.getDatePicker().setMinDate(newDate.getTime());
return da;
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
Here is a sample code:
DatePicker datePicker = (DatePicker) getView().findViewById(R.id.your_datepicker_id);
final Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, c.getMinimum(Calendar.HOUR_OF_DAY));
c.set(Calendar.MINUTE, c.getMinimum(Calendar.MINUTE));
c.set(Calendar.SECOND, c.getMinimum(Calendar.SECOND));
c.set(Calendar.MILLISECOND, c.getMinimum(Calendar.MILLISECOND));
Time now = new Time();
now.setToNow();
datePicker.updateDate(now.year, now.month, now.monthDay);
datePicker.setMinDate(c.getTimeInMillis());
Please note above that you must set MinDate value to the minimum time of the current day and not just the time which is 1 or 2 seconds behind the current time. This takes care of a well know problem of Datepicker in Android.
You can also try this:
da.getDatePicker().setMinDate(newDate.getTime()-(newDate.getTime()%(24*60*60*1000)));
It basically subtracts the current date by the current offset of the day.
Use setMinDate method (minimum API 11):
datePicker.setMinDate(System.currentTimeMillis() - 1000);
For more information regarding your topic :
How to disable dates before today date in DatePickerDialog Android?
How to disable past dates in Android date picker?
You can get date of 1 day after current day with this:
c.add(Calendar.DATE, 1);
So by using
c.add(Calendar.DATE, 1);
You are setting date of tomorrow as Minimum date. So just change that to
c.add(Calendar.DATE, 0);
And it should set today's date as minimum date in datepicker.
Hope it helps.
I am using the example from the documentation.
From the method
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
I want the string "Mon, Sep 14, 2014". How do I get it? Ideally I really want "Mon, Sep 14, 2014 10:30PM" but I can't find a combined date and time picker.
Here ya go date time combined.
This is no meant to be mean just that you said you were having trouble finding one.
how to find it : http://bit.ly/1doSxEf
Which one I use:
https://github.com/bendemboski/DateSlider
// try this way
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dt = chosenDate.toMillis(true);
CharSequence strDate = DateFormat.format("KKK MMM dd,yyyy", dt);
// mow use strDate..
}