转换INT VAL格式化" HH:MM:SS"使用JDK1.4 [英] Convert int val to format "HH:MM:SS" using JDK1.4

查看:193
本文介绍了转换INT VAL格式化" HH:MM:SS"使用JDK1.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是这种格式的int VAL转换为它的字符串对应的最有效的方法 - HH:MM:SS

What is the most efficient method of converting an int val to its string counterpart in this format - "HH:MM:SS"

10 becomes 00:00:10
70 becomes 00:01:10
3610 becomes 01:00:10

我需要这是JDK1.4兼容。

I need this to be JDK1.4 compliant.

我想出来的是一系列的if语句,然后根据当前INT VAL构建一个字符串。这不是很有效。有没有更好的办法?

What I've come up with is a series of if statements and then constructing a string based on the current int val. This is not very efficient. Is there a better way?

推荐答案

相反code一堆的,怎么样只有一行?

Instead of a heap of code, how about just one line?

String time = new SimpleDateFormat("HH:mm:ss")  
    {{setTimeZone(TimeZone.getTimeZone("UTC"));}}.format(new Date(3610 * 1000)); // "01:00:10"

P.S。我从来没有给我带来惊喜刚刚的多少code 的一些人写做最简单的事情

p.s. I never fails to amaze me just how much code some people write to do the simplest of things

的时区设置为UTC,因为SimpleDateFormat的使用你的区域设置格式化的日期。例如我的时区为GMT + 10,如果我格式化新的日期(0),时间部分是10:00:00。

The timezone is set to UTC, because SimpleDateFormat uses your locale to format the date. For example my timezone is GMT+10 and if I format new Date(0), the time part is "10:00:00".

对于那些谁不承认奇怪的语法,其中的时区被设置,它与一个实例块,这将会对建设执行的匿名类,构造函数完成后。

For those who don't recognise that odd syntax where the timezone is being set, it's an anonymous class with an "instance block", which gets executed on construction, after the constructor has finished.

这篇关于转换INT VAL格式化" HH:MM:SS"使用JDK1.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆