-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?

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
dfg
  • 27
  • 2
  • 3
  • 1
    what this number represent? seconds or milliseconds? – Abhishek May 29 '17 at 04:37
  • This [answer](https://stackoverflow.com/a/30994540/5513005) might help you. – Yash Karanke May 29 '17 at 04:37
  • @Abhishek, seconds. can you help me? please – dfg May 29 '17 at 04:38
  • 1
    Convert it to milliseconds and then use Date class to get what you want. – Abhishek May 29 '17 at 04:40
  • @Abhishek, can you help me and send to me code? please. send me code. i really need this – dfg May 29 '17 at 04:42
  • 1
    see `DateUtils#formatElapsedTime` – pskink May 29 '17 at 04:59
  • @Abhishek 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](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8+. Much of the java.time functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Feb 01 '18 at 03:41
  • @BasilBourque I will try and let you know. – Abhishek Feb 01 '18 at 07:16

5 Answers5

4
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
EKN
  • 1,886
  • 1
  • 16
  • 29
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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
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

badarshahzad
  • 1,227
  • 16
  • 25
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;
        }
    }
pradeep
  • 307
  • 1
  • 8
  • Just change the simpledateformat to below code .SimpleDateFormat formatter = new SimpleDateFormat("dd:HH:mm:ss", Locale.UK); – pradeep May 29 '17 at 05:01
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

Abhishek
  • 3,348
  • 3
  • 15
  • 34