if (v == btnEndTimePicker) {
final Calendar c = Calendar.getInstance() ;
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinutes = c.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(true, this,
(view, hour, minutes) -> {
return editStartTime.setText((hour + ":" + minutes), mHour, mMinutes);
});
timePickerDialog.show();
}
if (v == btnStartTimePicker) {
final Calendar c = Calendar.getInstance() ;
sHour = c.get(Calendar.HOUR_OF_DAY);
sMinutes = c.get(Calendar.MINUTE);
CalendarDialog timePickerDialog = new CalendarDialog(true, this,
(view, hour, minutes) -> {
return editStartTime.setText((hour + ":" + minutes), sHour, sMinutes);
});
timePickerDialog.show();
}
I'm not sure what to do here, I used a similar code to pop up a Calendar Date Dialog and choose a date that is then dropped into an EditText field. When I do it for TimePickerDialog it says it it's expecting a char[] not a String.
You Give Wrong Parameters to TimePickerDialog object its get
public TimePickerDialog (Context context,
TimePickerDialog.OnTimeSetListener listener,
int hourOfDay,
int minute,
boolean is24HourView)
replace this
TimePickerDialog timePickerDialog = new TimePickerDialog(true, this,
(view, hour, minutes) -> {
return editStartTime.setText((hour + ":" + minutes), mHour, mMinutes);
});
with
TimePickerDialog timePickerDialog = new TimePickerDialog(this,listener,2,25,true);
these are right Parameters for more check it out
https://developer.android.com/reference/android/app/TimePickerDialog
Related
I have 2 DatePickers, one is start date picker and the other is end date picker. I want to set the min date of the end DatePicker to the start date which user has selected.
I set the Data which user selected in Calander startDate and set this data as MinDate at end date.
I've tried in this way but it's not working :(
I can't use Mateiral Range Date Picker because I have to use specific theme.
Also I'm not good at programming so I'd appreciate it if you could explain it in detail.
ipDcEventStartDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
final Calendar c = Calendar.getInstance();
startYear = c.get(Calendar.YEAR);
startMonth = c.get(Calendar.MONTH);
startDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startYear = year;
startMonth = month+1;
startDay = dayOfMonth;
ipDcEventStartDay.setText(startYear + " - " + startMonth + " - " + startDay);
ipDcEventStartDay.setTextColor(Color.parseColor("#000000"));
}
}, startYear, startMonth, startDay);
dpd.show();
}
});
Calendar startDate = Calendar.getInstance();
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar c = Calendar.getInstance();
endYear = c.get(Calendar.YEAR);
endMonth = c.get(Calendar.MONTH);
endDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(DoubleCheckEventActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
endYear = year;
endMonth = month;
endDay = dayOfMonth;
ipDcEventEndDay.setText(endYear + " - " + (endMonth+1) + " - " + endDay);
ipDcEventEndDay.setTextColor(Color.parseColor("#000000"));
}
}, endYear,endMonth, endDay);
dpd.getDatePicker().setMinDate(startDate.getTimeInMillis());
dpd.show();
}
});
This code:
startDate.set(Calendar.YEAR, startYear);
startDate.set(Calendar.MONTH, startMonth);
startDate.set(Calendar.DAY_OF_MONTH, startDay);
Should be inside
ipDcEventEndDay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
Because startYear, startMonth and startDay are only set on ipDcEventStartDay.setOnClickListener(), so at the beginning they have some value but not the one that was set on ipDcEventStartDay.setOnClickListener()
So in ipDcEventEndDay.setOnClickListener() you have to set again on startDate the values that were picked on the other DatePickerDialog
How to get duration between selected time (using graphical date and time picker) and date and time now in seconds?
Both methods getSelectedDate() and getSelectedTime() look fine, selected date and time is fine. But there is something wrong with the method getCountDuration(). The value is incorrect. And I can't find an error.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
int YEAR = 0, DATE = 0, MONTH = 0, HOUR = 0, MINUTE = 0;
Button dateButton;
TextView dateTextView, timeTextView, textViewDateAndTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateButton = findViewById(R.id.dateButton);
dateTextView = findViewById(R.id.dateTextView);
timeTextView = findViewById(R.id.timeTextView);
textViewDateAndTime = findViewById(R.id.textViewDateTimeDuration);
dateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getSelectData();
getSelectTime();
getCountDuration();
}
});
}
public LocalDate getSelectData() {
final Calendar calendarDate = Calendar.getInstance();
YEAR = calendarDate.get(Calendar.YEAR);
MONTH = calendarDate.get(Calendar.MONTH);
DATE = calendarDate.get(Calendar.DATE);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int year, int month, int date) {
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.YEAR, year);
calendar1.set(Calendar.MONTH, month);
calendar1.set(Calendar.DATE, date);
String dateText = DateFormat.format("dd.MM.yyyy", calendar1).toString();
dateTextView.setText(dateText);
}
}, YEAR, MONTH, DATE);
datePickerDialog.show();
return null;
}
public LocalTime getSelectTime() {
Calendar calendar = Calendar.getInstance();
final LocalDateTime nowIs = LocalDateTime.now();
Date currentDate = new Date();
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");//("dd.MM.yyyy HH:mm"
final LocalDateTime dateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
HOUR = calendar.get(Calendar.HOUR);
MINUTE = calendar.get(Calendar.MINUTE);
boolean is24HourFormat = DateFormat.is24HourFormat(this);
final TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {
Log.i(TAG, "onTimeSet: " + hour + minute);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(Calendar.HOUR, hour);
calendar1.set(Calendar.MINUTE, minute);
String dateText = DateFormat.format("hh:mm", calendar1).toString();
timeTextView.setText(dateText);
String dateNowText = formatter.format(dateTime).toString();
}
}, HOUR, MINUTE, is24HourFormat);
timePickerDialog.show();
return null;
}
public void getCountDuration() {
final Calendar setDate = Calendar.getInstance();
setDate.set(YEAR, MONTH, DATE, HOUR, MINUTE, 0);
long subs = Calendar.getInstance().getTimeInMillis() - setDate.getTimeInMillis();
textViewDateAndTime.setText(subs + " duration in milliseconds");
}
}
I am trying to set a datepicker by timezone specified by the app. I have succeed somewhat in applying the current selection and minDates but I can't get today date (bold number on date picker dialogue) to change. I have uploaded a picture which shows datepicker using getDefult timezone to set today date instead of specified timezone.
How can I specify today date?
billDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
billDateDialog.show();
billDateDialog.updateDate(billDateCal.get(Calendar.YEAR), billDateCal.get(Calendar.MONTH), billDateCal.get(Calendar.DAY_OF_MONTH));
}
});
billDateDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
long correctedTime = new DayLightSavings(BillsAdd.this, year, monthOfYear, dayOfMonth, timeZone).getCorrectedTime();
billDateCal.setTimeInMillis(correctedTime);
billDate.setText(dateFormatter.format(billDateCal.getTimeInMillis()));
}
},billDateCal.get(Calendar.YEAR), billDateCal.get(Calendar.MONTH), billDateCal.get(Calendar.DAY_OF_MONTH));
long oneDay = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
long oldRawOffset = TimeZone.getDefault().getRawOffset();
long newRawOffset = timeZone.getRawOffset();
long rawOffset = oldRawOffset - newRawOffset;
billDateDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() + oneDay - rawOffset);
You can check this code.This might be helpful for you...
//Create calendar instance
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
final DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
tvStartdate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
tvStartdate.setTextColor(Color.BLACK);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formatedDate = sdf.format(new Date(year - 1900, monthOfYear, dayOfMonth));
sd = formatedDate;
}
}, year, month, day);
dpd.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
dpd.show();
I want to store a time in only 12 hours format.
the Time Picker is working perfectly but i just want to set AM or PM after the selected time in EditText.
this is my TimePicker theme
Here, my complete code
starttime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Initialize a new time picker dialog fragment
DialogFragment dFragment = new TimePickerFragment();
// Show the time picker dialog fragment
dFragment.show(getActivity().getFragmentManager(),"Time Picker");
}
});
TimePickerDialog.java
package com.Weal.sachin.omcom;
/**
* Created by sachin on 2/1/2017.
*/
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.TextView;
import android.app.DialogFragment;
import android.app.Dialog;
import java.util.Calendar;
import android.widget.TimePicker;
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
/*
Creates a new time picker dialog with the specified theme.
TimePickerDialog(Context context, int themeResId,
TimePickerDialog.OnTimeSetListener listener,
int hourOfDay, int minute, boolean is24HourView)
*/
// TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT
TimePickerDialog tpd = new TimePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);
// TimePickerDialog Theme : THEME_DEVICE_DEFAULT_DARK
TimePickerDialog tpd2 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,hour,minute,false);
// TimePickerDialog Theme : THEME_HOLO_DARK
TimePickerDialog tpd3 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_DARK,this,hour,minute,false);
// TimePickerDialog Theme : THEME_HOLO_LIGHT
TimePickerDialog tpd4 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false);
// TimePickerDialog Theme : THEME_TRADITIONAL
TimePickerDialog tpd5 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_TRADITIONAL,this,hour,minute,false);
// Return the TimePickerDialog
return tpd;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
// Do something with the returned time
TextView tv = (TextView) getActivity().findViewById(R.id.start_time);
tv.setText( hourOfDay + ":" + minute );
}
}
use this logic to find am pm
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
String AM_PM ;
if(hourOfDay < 12) {
AM_PM = "AM";
} else {
AM_PM = "PM";
}
}
try it will helps you,
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
String hourString = "";
if(hourOfDay < 12) {
hourString = hourOfDay < 10 ? "0"+hourOfDay : ""+hourOfDay;
} else {
hourString = (hourOfDay - 12) < 10 ? "0"+(hourOfDay - 12) : ""+(hourOfDay - 12);
}
String minuteString = minute < 10 ? "0"+minute : ""+minute;
String secondString = second < 10 ? "0"+second : ""+second;
String am_pm = (hourOfDay < 12) ? "am" : "pm";
String time = hourString+":"+minuteString + " " + am_pm;
time.setTextColor(getResources().getColor(R.color.dark_blue));
time.setText(time);
}
This answer would be the best Option with few lines of code.
#Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
calendar.set(Calendar.SECOND,second);
textViewStartTime.setText(startTimeFormat().format(calendar.getTime()));
}
public SimpleDateFormat startTimeFormat(){
return this.startTimeFormat = new SimpleDateFormat("hh:00 a");
}
OutPut will be :- 12:00 AM
TimePicker shows current time as default in TimePicker, but what if i want to set default time in time picker as per my requirement.
Like its 11:10 when i am writing this, but in TimePicker i like to show 01:00 as default (my mean 2 hours difference, and minutes should be 00 only)
static final int TIME_DIALOG_ID = 1;
public int year,month,day,hour,minute;
private int mYear, mMonth, mDay,mHour,mMinute;
public TimePickerActivity() {
// Assign current Date and Time Values to Variables
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
String formattedMinutes = "" + min;
String formattedHour = "" + hourOfDay;
if (hourOfDay < 10) {
formattedHour = "0" + hourOfDay;
}
if (min < 10) {
formattedMinutes = "0" + min;
}
textTime.setText(formattedHour + ":" + formattedMinutes);
}
};
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
TimePickerDialog timePickerDialog = new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false);
return timePickerDialog;
}
return null;
}
You can do this with following code:
private TimePicker timePicker;
timePicker = (TimePicker) dialog.findViewById(R.id.timePickerDialog);
if(DateFormat.is24HourFormat(getActivity()){
timePicker.setIs24HourView(true);
}else {
timePicker.setIs24HourView(false);
}
// here you can define your hour and minute value.
timePicker.setCurrentHour(hour);
timePicker.setCurrentMinute(minute);
You can try below code
SimpleDateFormat sdf = new SimpleDateFormat("hh:ss");
Date date = null;
try {
date = sdf.parse("07:00");
} catch (ParseException e) {
}
Calendar c = Calendar.getInstance();
c.setTime(date);
TimePicker picker = new TimePicker(getApplicationContext());
picker.setCurrentHour(c.get(Calendar.HOUR_OF_DAY));
picker.setCurrentMinute(c.get(Calendar.MINUTE));