How to convert number to hour and minute in android
Asked Answered
D

5

-1

In my application i should show hour and minute and i get this numbers from server with this sample :
Json :

{
  data:{
    time:84561
  }
}

i should get this number from time and show it with this format

**hh : mm : ss**

I can get number of time, but i can't convert this to **hh : mm : ss** .

How can i it?

Dibbrun answered 29/5, 2017 at 4:36 Comment(8)
what this number represent? seconds or milliseconds?Alphonsealphonsine
This answer might help you.Flap
@Abhishek, seconds. can you help me? pleaseDibbrun
Convert it to milliseconds and then use Date class to get what you want.Alphonsealphonsine
@Abhishek, can you help me and send to me code? please. send me code. i really need thisDibbrun
see DateUtils#formatElapsedTimeKasi
@Alphonsealphonsine No, not Date. The troublesome old date-time classes such as java.util.Date & java.util.Calendar are now legacy, supplanted by the java.time classes built into Java 8+. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the ThreeTen-Backport project. Further adapted for earlier Android in ThreeTenABP project. See How to use ThreeTenABP….Meningitis
@BasilBourque I will try and let you know.Alphonsealphonsine
I
5
long timeSec= 84561;// Json output
int hours = (int) timeSec/ 3600;
int temp = (int) timeSec- hours * 3600;
int mins = temp / 60;
temp = temp - mins * 60;
int secs = temp;

String requiredFormat = hours+ ": "+mins+": "+secs;//hh:mm:ss formatted string
Inman answered 29/5, 2017 at 4:43 Comment(0)
M
2

Java 9 answer

    Duration time = Duration.ofSeconds(87561);
    String hms = String.format("%02d : %02d : %02d", 
            time.toHoursPart(), 
            time.toMinutesPart(), 
            time.toSecondsPart());

Unfortunately in Java 8 Duration does not lend itself well to formatting. The methods I use in the snippet are introduced in Java 9 and will calculate the values for hh, mm and ss. Then String.format() does the rest.

I know you cannot use this on Andriod (yet), but I wanted to have this answer stand here for others who can use Java 9, now or in the future.

Marte answered 29/5, 2017 at 12:35 Comment(0)
G
1

Very simple

If this is unix time then it will be converted into human readable time format with this snippet.

String relavtiveTimeString = String.valueOf(DateUtils.getRelativeTimeSpanString(unixTime));

You can use new Date(unix); and with below function you can get formatted date. You can format in different style.

//This mehtod convert the date into standard date like : 2017-12-23
    public static String getformateDate(Date dateObject) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(dateObject);
    }

For more information check this link already answer : Convert unix time stamp to date in java

Referance:

  1. https://developer.android.com/reference/android/text/format/DateUtils.html

  2. https://developer.android.com/reference/android/text/format/Time.html

Gash answered 31/1, 2018 at 20:32 Comment(0)
P
0

Pass your Number or timestamp and convert to milliseconds for hour and minute.use the below code.

 public static String getCurrentTimeStamp(String mCurrentTime) {
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy  hh:mm a z");
        TimeZone tz = TimeZone.getDefault();
        format.setTimeZone(tz);
        //  System.out.println("TimeZone   "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
        SimpleDateFormat dateParser = new SimpleDateFormat("dd/MM/yyyy  hh:mm a z");
        Date dateTime = null;
        try {
            dateTime = dateParser.parse(format.format(Long.parseLong((mCurrentTime)) * 1000));
            Log.e("Starttime", "Starttime" + format.format(dateTime));
            return format.format(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
Pardon answered 29/5, 2017 at 4:41 Comment(1)
Just change the simpledateformat to below code .SimpleDateFormat formatter = new SimpleDateFormat("dd:HH:mm:ss", Locale.UK);Pardon
A
0

Try This Logic Use it As per Requirement

public class Test {

public static void main(String[] args) {

    String json = "{\n" +
            "  data:{\n" +
            "    time:84561\n" +
            "  }\n" +
            "}";

    Date date = new Gson().fromJson(json, Date.class);

    long milli = date.getTime() * 1000;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

    System.out.println(simpleDateFormat.format(new java.util.Date(milli)));

}

class Date implements Serializable{

    int time;


    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}

Output

05:30:00

If you want to download Gson jar download it from

here

Alphonsealphonsine answered 29/5, 2017 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.