Java JFormattedTextfield时间格式 [英] Java JFormattedTextfield time formatting

查看:147
本文介绍了Java JFormattedTextfield时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在代码中使用时间有些麻烦:

I got some troubles using time in my code:

txtDauer = new JFormattedTextField();
txtDauer.setFormatterFactory(new DefaultFormatterFactory(
    new DateFormatter(DateFormat.getTimeInstance())));

当我对上面的FormattedTextfield使用setValue(0)时,文本字段显示01:00:00而不是00:00:00.

When I use setValue(0) to the above FormattedTextfield the textfield shows 01:00:00 instead of 00:00:00.

使用此方法在代码的另一行也会出现相同的问题:

The same problem occurs also at another line of the code with this method:

public static String convertLongToString(Long time) {
    String strtime = new SimpleDateFormat("HH:mm:ss").format(time);
    return strtime;
}

将此方法与"0"一起使用将返回strtime ="01:00:00".

Using this method with "0" it returns strtime = "01:00:00".

推荐答案

问题是TimeZone ,您的默认计算机的TimeZone是GMT +1.

The problem is the TimeZone, your default computer's TimeZone is GMT +1.

解决方案是将GMT设置为+0:

The solution is setting GMT to +0:

DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("GMT"));

运行并预览.

这篇关于Java JFormattedTextfield时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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