I want the date in AM/PM format. Examples suggest to use a SimpleDateFormat with a or aa but this is simply not working for me.
First I set the day. Then I set view to visible for start time.
Result is format of Wed May 20 01:00:00 EDT 2020 want something like Wed May 20 01:00:00 AM EDT 2020
This applies to calendar.getTime() in the setStartTime method. The first method is only included because the day selected is linked as a global variable. Problem lies within the second method.
I could probably manually change the final date as string and convert it back but I don't want to add unnecessary complexity. I want to find out why Calendar won't succeed since this should be extremely basic.
In the end my goal is to add the date to firebase. It must be uniform across Android, IOS and Web. Therefore I need the date to be in this exact format.
Here is code:
SimpleDateFormat serviceSETimeFormat = new SimpleDateFormat("MMM dd, yyyy HH:mm aa");
public void setStartDay(View view)
{
Calendar calendar = Calendar.getInstance();
DatePickerDialog picker = new DatePickerDialog(ServiceCreation.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
GregorianCalendar mCal = new GregorianCalendar(year, monthOfYear, dayOfMonth);
Date dateForDisplay = mCal.getTime();
int startHourIndex = getThirdSpaceIndex(dateForDisplay.toString());
//SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy");
serviceSETimeFormat.setCalendar(mCal);
Date dateFormatted = null;
try{
dateFormatted= serviceSETimeFormat.parse(serviceSETimeFormat.format(mCal.getTime()));
currBaseStartDate=dateFormatted;
}
catch(ParseException p){
Log.wtf("SC","Parse exc34");
}
Log.wtf("SC","Here is orig date: "+dateFormatted);
//Don't show hh/mm/ss (these are held as 0)
startDayView.setText(dateForDisplay.toString().substring(0,startHourIndex));
startEndTimeWrapper.setVisibility(View.VISIBLE);
}
}, calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
long currentTime = new Date().getTime();
picker.getDatePicker().setMinDate(currentTime);
picker.show();
}
public void setStartTime(View view) {
TimePickerDialog mTimePicker = new TimePickerDialog(ServiceCreation.this,
android.R.style.Theme_Holo_Light_Dialog_NoActionBar, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
String minuteAsS = Integer.toString(selectedMinute);
if(minuteAsS.length()==1){
minuteAsS="0"+minuteAsS;
}
if(selectedHour==0){
startTimeView.setText((selectedHour+12)+":"+minuteAsS+" AM");
}
else if(selectedHour<12){
startTimeView.setText(selectedHour+":"+minuteAsS+" AM");
}
else if(selectedHour==12){
startTimeView.setText((selectedHour)+":"+minuteAsS+" PM");
}
else{
startTimeView.setText((selectedHour-12)+":"+minuteAsS+" PM");
}
(findViewById(R.id.endTimeWrapper)).setVisibility(View.VISIBLE);
Calendar calendar = new GregorianCalendar();
calendar.setTime(currBaseStartDate);
calendar.set(Calendar.MINUTE, selectedMinute);
Log.wtf("SC","Hour before condition: "+selectedHour);
if(selectedHour>=12){
if(selectedHour!=12){
Log.wtf("SC","Decrementing hour");
selectedHour-=12;
}
Log.wtf("SC","Hour set: "+selectedHour);
calendar.set(Calendar.AM_PM,Calendar.PM);
calendar.set(Calendar.HOUR_OF_DAY,selectedHour);
}
else{
calendar.set(Calendar.AM_PM,Calendar.AM);
calendar.set(Calendar.HOUR_OF_DAY,selectedHour);
}
startTime=calendar.getTime();
Log.wtf("SC","Here is orig start time: "+startTime);
try{
startTime=serviceSETimeFormat.parse(serviceSETimeFormat.format(startTime));
Log.wtf("SC","Here is parsed start time: "+startTime);
}
catch(ParseException e){
Log.wtf("SC","START DATE PARSE EXCEPTION");
}
}
}, 12, 0, false);
mTimePicker.setTitle("Select Start Time: ");
mTimePicker.show();
}