0

I am using the following answer to validate the time entered into a textbox:

Parsing user time input in Java/GWT

This returns milliseconds in Long format. So now I want to convert that to 24 hour format. So I use this suggestion:

long startTime = parseTime(textBoxStartTime.getText());
long second = (startTime / 1000) % 60;
long minute = (startTime / (1000 * 60)) % 60;
long hour = (startTime / (1000 * 60 * 60)) % 24;
String time = String.format("%02d:%02d:%02d", hour, minute, second);

Based on:

java convert milliseconds to time format

However, I get the following error when I compile:

[ERROR] Errors in 'file:/C:/Users/Glyndwr/workspace/AwardTracker_N/src/org/AwardTracker/client/HikeDetailsView.java'
      [ERROR] Line 387: The method format(String, long, long, long) is undefined for the type String

I have also tried:

String startTimeString = DateTimeFormat.getFormat("HH:mm").format(startTime);

Which gives the error:

Thee method format(Date) in the type DateTimeFormat is not applicable for the arguments (long)
Community
  • 1
  • 1
Glyn
  • 1,933
  • 5
  • 37
  • 60

3 Answers3

1

GWT does not emulate all the methods in java.lang.String. So you cannot use the method public static String format(String format, Object... args) in GWT.

You can use the following code instead.

StringBuilder sb=new StringBuilder();
sb.append(hour).append(":").append(minute).append(":").append(second);
String time =sb.toString();
Syam Kumar S
  • 832
  • 2
  • 8
  • 26
0

It should be:

String startTimeString = DateTimeFormat.getFormat("HH:mm").format(new Date(startTime));

If time zone is important, you need to pass TimeZone to the format method in addition to Date.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • Hi Andrei, thank you very much for your help and this almost works. However, all times are ahead 11 hours (e.g., if I enter 02:00 then the milliseconds returned are 7200000 (correct) which should be 02:00 hours, however 13:00 is returned from the formatting). Time zone is not important to me. If I enter 2 am I want 02:00 returned. – Glyn Mar 27 '16 at 04:34
0

The following code works:

long startTime = parseTime(textBoxStartTime.getText());
int i = (int) startTime;
int minute = (i / (1000 * 60)) % 60;
String formattedMinute = NumberFormat.getFormat("00").format(minute);
int hour = (i / (1000 * 60 * 60)) % 24;
String formattedHour = NumberFormat.getFormat("00").format(hour);
String time = null;
time = formattedHour + ":" + formattedMinute;
textBoxStartTime.setText(time);
Glyn
  • 1,933
  • 5
  • 37
  • 60