2

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".

Cœur
  • 37,241
  • 25
  • 195
  • 267
andyRandy
  • 93
  • 1
  • 3
  • 10

2 Answers2

2

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.

d1e
  • 6,372
  • 2
  • 28
  • 41
1

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

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Unfortunately, I cannot replace my JFormattedTextfield with a JSpinner for 2 reasons: first, the textfield is not editable by the user and second its in fact a timer which can be controlled with start, pause and stop buttons. But anyway, thanks for your effort. – andyRandy Feb 28 '12 at 14:58