将int val转换为格式" HH:MM:SS& quot;使用JDK1.4 [英] Convert int val to format "HH:MM:SS" using JDK1.4

查看:56
本文介绍了将int val转换为格式" HH:MM:SS& quot;使用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?

推荐答案

不是一行代码,而是一行吗?

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.我从来没有让我惊讶,只是多少代码有人写了做最简单的事情

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,如果我格式化 new Date(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& quot;使用JDK1.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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