How to convert number to hour and minute in androi

2019-09-25 09:39发布

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?

5条回答
再贱就再见
2楼-- · 2019-09-25 10:18

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;
        }
    }
查看更多
劳资没心,怎么记你
3楼-- · 2019-09-25 10:27

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

查看更多
Ridiculous、
4楼-- · 2019-09-25 10:31

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.

查看更多
ら.Afraid
5楼-- · 2019-09-25 10:36

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

查看更多
虎瘦雄心在
6楼-- · 2019-09-25 10:39
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
查看更多
登录 后发表回答