Hello I have to set date as per user selected time AM/PM
My Code for time
private TimePickerDialog.OnTimeSetListener timeListener1 =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hours1=hourOfDay;
min1=minute;
SimpleDateFormat formate = new SimpleDateFormat("HH:mm");
String newTime = formate.format(System.currentTimeMillis());
updateTime1();
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hours = c.get(Calendar.HOUR);
int years = c.get(Calendar.YEAR);
int months = 1 + c.get(Calendar.MONTH);
int days = c.get(Calendar.DAY_OF_MONTH);
int AM_orPM = c.get(Calendar.AM_PM);
try{
if ((c.get(Calendar.AM_PM) == 1) )
{
String PM = "";
if (AM_orPM == 1)
{
PM = "PM";
}
populateSetDate2(days, months, years);
Cabdate.setText("Refreshed on " + days + "-"
+ months + "-" + years + PM);
}
else if ((c.get(Calendar.AM_PM) == 0) )
{
String AM = "";
if (AM_orPM == 0)
{
AM = "AM";
}
populateSetDate2(ddd1, mmm1, ddd1);
Cabdate.setText("Refreshed on " + ddd1 + "-"
+ mmm1 + "-" + yyy1 + AM);
}
}
catch (Exception e){}
/* if(hours1>=hours)
{
hours=hours1+5;
Toast.makeText(AirlinecabBooking.this, "invalid",Toast.LENGTH_LONG).show();
}*/
}
};
For Date:
@SuppressLint({ "ValidFragment", "NewApi" })
public class SelectDateFragment2 extends DialogFragment implements
OnDateSetListener {
@SuppressLint("NewApi")
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Calendar calendar = Calendar.getInstance();
yyy1 = calendar.get(Calendar.YEAR);
mmm1 = calendar.get(Calendar.MONTH);
ddd1 = calendar.get(Calendar.DAY_OF_MONTH);
mmm1= mmm1 + 1;
Calendar plus10days = Calendar.getInstance();
plus10days.add(Calendar.DAY_OF_YEAR, 31);
d = plus10days.get(calendar.DAY_OF_YEAR);
calendar.get(Calendar.AM_PM);
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
@Override
public void onDateSet(DatePicker arg0, int yy, int mm, int dd) {
// TODO Auto-generated method stub
populateSetDate2(yy, mm + 1, dd);
}
}
public void populateSetDate2(int year, int month, int day) {
if (year == yyy1) {
if ((month >= mmm1) && (year == yyy1)) {
if( day > ddd1 && day < d )
{
Cabdate.setText(day + "/" + month + "/" + year);
final_date=year+"-"+month+"-"+day;
System.out.println(final_date);
}
else
{
Toast.makeText(getApplicationContext(),
"Sorry Problem with date. ", Toast.LENGTH_SHORT)
.show();
}
}
else if (month >= mmm1 && year == yyy1)
{
Cabdate.setText(day + "/" + month + "/" + year);
final_date=year+"-"+month+"-"+day;
System.out.println(final_date);
}
else
{
Toast.makeText(getApplicationContext(),
"Sorry Problem with month. ", Toast.LENGTH_SHORT)
.show();
}
}
else
{
Toast.makeText(getApplicationContext(),
"Sorry Problem with year. ", Toast.LENGTH_SHORT).show();
}
Problem is when I select any time format am/pm it sets date as a current date even if I choose future date..
Here Cabdate
is my date textview
Please help me Thank You
Calendar.getInstance();
returns a Calendar
instance initilalized with the current time in the default time zone. You should use setTimeInMillis(long millis)
or setTime(Date date)
to change the time in your Calendar
instance.
When you dismiss your DatePiker
int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year = datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Use this:
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
Calendar c = Calendar.getInstance();
int seconds = c.get(Calendar.SECOND);
Date mydate = Calendar.getInstance().getTime();
SimpleDateFormat formatter = new SimpleDateFormat(
"MMM dd yyyy hh:mm:ss a");
String time = formatter.format(mydate);
// Toast.makeText(MainActivity.this, time,
// Toast.LENGTH_SHORT).show();
// textExpiry.setText(formatter.format(dateTime.getTime()));
mTextTime.setText(time);
}
});
}
};
t.schedule(scanTask, 1000, 1000);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.