Java JFormattedTextfield time formatting

家住魔仙堡 提交于 2019-12-12 20:34:47

问题


I got some troubles using time in my code:

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

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

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


回答1:


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

The solution is setting GMT to +0:

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

Run and preview.




回答2:


For Date, Date & Time or Time use JSpinner with SpinnerDateModel, example about using SimpleDateFormat



来源:https://stackoverflow.com/questions/9483716/java-jformattedtextfield-time-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!