user13928463
user13928463

Reputation:

How to change the clock display system from 24 to 12 hours android

I have a student table in the database.In the student table I have a field named time and data type of this field is time. Now the time is saved by the following code:

<?php
 
    include 'con.php';

        
date_default_timezone_set("Asia/Muscat");  

        $sql = "insert into student(time)values(now())";

        if(mysqli_query($con, $sql))
        {
            
            
        } else {
            
        }
        
    
    mysqli_close($con);
?> 

The problem is the data is saved in a 24-hour system. Like that:

enter image description here

This image from my app through(recyclerview).I inquire about data from the database through a connection of(Volley.)

I need to display time by 12-hour system.And save it by 12 hours system also.How can I do that?

I I view data through (recyclerview)by this code:

 @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        ItemHome currentItem = mExampleList.get(position);
     
        String time = currentItem.getTime();

        holder.text_view_time.setText(time);


Upvotes: 0

Views: 1282

Answers (2)

Rajnish Sharma
Rajnish Sharma

Reputation: 388

SimpleDateFormat formatDate = new SimpleDateFormat("hh:mm:ss a");

h is used for AM/PM times (1-12).

H is used for 24 hour times (1-24).

a is the AM/PM marker

Remember to use "hh" with "a" and not "HH"

Upvotes: 1

Nima Ganji
Nima Ganji

Reputation: 601

Parse your date in saved format, then convert it to 12-hour format.

DateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd, HH:mm:ss", Locale.US);
DateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd, hh:mm:ss aa", Locale.US);

input = "2020-07-19, 14:14:14"
Date date = inputDateFormat .parse(input);
String output = outputDateFormat .format(date);

"aa" in outputDateFormat makes 12-hour format for you.

Upvotes: 1

Related Questions