last day (30 days later) » 

13:31
0
Q: SimpleDateFormat alternatively formatting date from 24hr format to 12hr format in Android/Kotlin

Vivek YadavVery weird. I know. This is the executing code. Before executing the timePickerDialog, I execute datePickerDialog which fills in c[Calendar.YEAR], c[Calendar.MONTH], c[Calendar.DAY_OF_MONTH], but that code is spaghetti and probably unrelated. I'll append if requested. val timePickerDialog = Tim...

You changed the date in your second try. Did you assign picked date from DatePicker to Calendar.getInstance() you're using before opening TimePicker. Can you check this?
@rupinderjeet Yes. I indeed assign the picked date from DatePicker to calendar instance first before using it.
Can you check by formatting with "AM / PM" if 02:30 is from morning.
@rupinderjeet I indeed do that as specified in the code. Had it been a 12 hour format, the AM/PM would've been appended to the TextView text. Also, the toast suggests that code is correctly entering the 24-hour if clause
Also, In your posted code, TimePicker is being created with old date because you create TimePicker before creating DatePicker. By the time a date is selected, and you change the Calendar, TimePicker is already created. So, newly selected date is not given to already created TimePicker.
13:31
@rupinderjeet I created timePicker before so that I could reference it in the lambda I passed to datePicker. (I wanted to do timePicker.show() from datePicker lambda). TimePicker onDateChanged method is only called after date has been selected, so that shouldn't be a problem.
That is a problem. Declare your pickers globally.
Or create a method that returns TimePicker.
Ok. So all I need to do is declare them as members of the class?
Is it possible to change the initial date of the pickers before inflating them?
Or do this/

fun onDateSet() {
// apply selection to calendar

val timePicker = createTimePicker(onTimeSetListener)
timePicker.show()
}
Ok. I'm going to first try making them members of the class and see if that helps. If not, I'll try this.
Remember TimePicker must be created after onDateSet for it to work with your existing code.
13:36
Ok.
Also, this is going to be faulty code in long run. Basically, you'd create both pickers globally. Implement listeners on your adapter. And, set values(date, time) on global pickers before calling them.
This is kinda overwhelming. Can you please give me a step solution?
Also, would it be idiomatically correct to implement listeners on adapter class when I can just use a lambda instead of them? Looks like that would just pollute the adapter class namespace
14:04
class ContactAdapter(
private val context: Context,
private val contact_name: String?,
private val items: List<Contact>
) :
RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
private val c = Calendar.getInstance()
fun createDatePicker(onSetDate: (DatePicker, Int, Int, Int) -> Unit) = DatePickerDialog(
context,
onSetDate,
c[Calendar.YEAR],
c[Calendar.MONTH],
c[Calendar.DAY_OF_MONTH]
)

fun createTimePicker(onSetTime: (TimePicker, Int, Int) -> Unit) = TimePickerDialog(
context,
onSetTime,
c[Calendar.HOUR],
Ok, I've added made the code a little bit more manageable and readable
14:16
Hello
Yeah
I've changed my code a bit
Move this code:

val timePicker = createTimePicker{
_, hour, minute ->
c[Calendar.HOUR] = hour
c[Calendar.MINUTE] = minute
}
// Do time picking stuff
timePicker.show()

return if (DateFormat.is24HourFormat(context)){
Toast.makeText(context, "Chose 24-hour clause", Toast.LENGTH_SHORT).show()
SimpleDateFormat("dd mmm, yyyy 'at' HH:mm", Locale.getDefault()).format(c.time)}
else{
Toast.makeText(context, "Chose 12-hour clause", Toast.LENGTH_SHORT).show()
SimpleDateFormat("dd mmm, yyyy 'at' hh:mm aa", Locale.getDefault()).format(c.time)}
Into DateSet Listener.
Ok.
So, something like:
val datePicker = createDatePicker{_, year, month, date ->
c[Calendar.YEAR] = year
c[Calendar.MONTH] = month
c[Calendar.DAY_OF_MONTH] = date
val timePicker = createTimePicker{
_, hour, minute ->
c[Calendar.HOUR] = hour
c[Calendar.MINUTE] = minute
}
// Do time picking stuff
timePicker.show()
}
// Do date picking stuff
datePicker.show()
?
I have ensured that calendar is not accessed before setting of both of them, though, so I don't think this would be necessary.
nope nevermind it was not synchronised
This is synchronised now.
Also, I think following code returns before selecting time.
return if (DateFormat.is24HourFormat(context)){
Toast.makeText(context, "Chose 24-hour clause", Toast.LENGTH_SHORT).show()
SimpleDateFormat("dd mmm, yyyy 'at' HH:mm", Locale.getDefault()).format(c.time)}
else{
Toast.makeText(context, "Chose 12-hour clause", Toast.LENGTH_SHORT).show()
SimpleDateFormat("dd mmm, yyyy 'at' hh:mm aa", Locale.getDefault()).format(c.time)}
I also had to shift the return statement inside
yep
This is what it looks like now:
14:23
Even then it wouldn't work I think, you might need a Callback interface and pass it to your dateToString method.
private fun setDateTime() {
val datePicker = createDatePicker{_, year, month, date ->
c[Calendar.YEAR] = year
c[Calendar.MONTH] = month
c[Calendar.DAY_OF_MONTH] = date

val timePicker = createTimePicker{
_, hour, minute ->
c[Calendar.HOUR] = hour
c[Calendar.MINUTE] = minute
}

// Do time picking stuff
timePicker.show()

itemView.txtGreetingTime.text = if (DateFormat.is24HourFormat(context)){
Toast.makeText(context, "Chose 24-hour clause", Toast.LENGTH_SHORT).show()
SimpleDateFormat("dd mmm, yyyy 'at' HH:mm", Locale.getDefault()).format(c.time)}
Ok yes.
The TextView is set inside now.
This will work for your code
Well done
Ok. Going to check.
14:24
Wait
Do this

itemView.txtGreetingTime.text = if (...

inside TimeSet listener
There's still a problem
oh yeah
The problem persists :(
It's all synchronised now but the problem of suddenly showing, say 6:30 instead of 18:30 every second time is still there
lemme try
Also, the month format is a bit weird.
If you want, I can compress my project and send you a .rar somehow if that would be easier?
Nevermind that sounds very suspicious.
Ok the month format fix was easy, just changed "dd mmm, yyyy" to "dd MMM, yyyy"
OK, I've discovered something really weird.
I think it's a problem with the TimePicker
When I pick, say 18:30 on the TimePicker
then the next time the TimePicker is by default on 6:30. If you let it remain on 6:30, the TextView would still say "... at 18:30". But, if you change it to 18:30, the TextView reads "... at 6:30".
I also tried first selecting 18:30 as the time, then next 7:30 as the time, but the TextView read 19:30 despite that. Then selected 9:30, got 21:30. But as soon as I selected 22:30, I got 10:30
The effects of the buttons seem to have flipped somehow
It's either a problem in the SimpleDateFormat, formatting it wrong, or TimePicker
choosing the time wrong
I logged calendar.time, and it looks like the time in it is consistent with the output in TextView. So that means SimpleDateFormat isn't meddling in between.
Which only means that TimePicker indeed is messing things up somehow
14:50
Do not send project code.
ok ok.
I am writing a snippet.
15:05
Added an answer
Ok. I'll test it now.
added explanation
Thanks
It worked!
Thank you too
bye.

  last day (30 days later) »