2017-05-29 62 views
-3

在我的應用程序應該顯示小時和分鐘,我得到這個數字從服務器此示例:
的Json如何數轉換爲小時和分鐘的Android

{ 
    data:{ 
    time:84561 
    } 
} 

我應該從時間得到這個,並用這種格式

**hh : mm : ss** 
表現出來210

我可以得到的時間數,但我不能將其轉換爲**hh : mm : ss**

我該怎麼辦?

+1

這個數字代表什麼?幾秒或幾毫秒? – Abhishek

+0

This [answer](https://stackoverflow.com/a/30994540/5513005)可能會幫助你。 –

+0

@Ahhishek,秒。你可以幫我嗎?請 – dfg

回答

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

傳遞您的數字或時間戳並將時間和分鐘轉換爲毫秒。使用下面的代碼。

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; 
     } 
    } 
+0

只需將simpledateformat更改爲下面的代碼.SimpleDateFormat formatter = new SimpleDateFormat(「dd:HH:mm:ss」,Locale.UK); – pradeep

0

嘗試這個邏輯用它按規定

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; 
    } 
} 

輸出

五時三十分00秒

如果您想從

here

1

Java的9答案

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

不幸的是在Java中8 Duration並不太適合於格式下載GSON罐子下載。我在片段中使用的方法在Java 9中引入,並將計算hh,mm和ss的值。然後String.format()做其餘的。

我知道你不能在Andriod上使用它,但是我想讓這個答案在這裏表達給那些現在或將來可以使用Java 9的人。

0

很簡單

如果這是UNIX時間那麼它將被轉換成可讀的時間格式與此片段。

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

您可以使用new Date(unix);和下面的功能,你可以得到格式化的日期。你可以用不同的風格進行格式化

//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); 
    } 

欲瞭解更多信息,請檢查該鏈接已經回答: Convert unix time stamp to date in java

全球化志願服務青年:

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

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

相關問題