SimpleDateFormat does not handle leading blanks for HHmmss format

Hi,

I came accross a piece of code :

import java.util.*;

import java.io.*;

import java.text.*;

public class SDFTest{

public static void main(String[] args) {

try{

System.out.println("\"102030\" --> " + getTime("102030"));

System.out.println("\"002030\" --> " + getTime("002030"));

System.out.println("\" 12030\" --> " + getTime(" 12030"));

System.out.println("\" 2030\" --> " + getTime(" 2030"));

System.out.println("\"\" --> " + getTime(""));

}catch(Exception e){

e.printStackTrace();

}

}

private static String getTime(String time_str){

SimpleDateFormat sdf = new SimpleDateFormat("HHmmss", new Locale("ja", ""));

ParsePosition pp = new ParsePosition(0);

sdf.setLenient(false);

Date date = sdf.parse(time_str, pp);

return sdf.format(date);

}

}

The output from this code using java 1.4 is as follows

"102030" --> 102030

"002030" --> 002030

" 12030" --> 012030

java.lang.NullPointerException

at java.util.Calendar.setTime(Calendar.java:1032)

at java.text.SimpleDateFormat.format(SimpleDateFormat.java:785)

at java.text.SimpleDateFormat.format(SimpleDateFormat.java:778)

at java.text.DateFormat.format(DateFormat.java:314)

at testSimpleDateFormat.getTime(testSimpleDateFormat.java:23)

at testSimpleDateFormat.main(testSimpleDateFormat.java:11)

So, with a string like " 2030", I get a null pointer exception. Is the string format incorrect for the pattern "HHmmss" ?

[1614 byte] By [250884a] at [2007-11-26 17:46:10]
# 1
You are getting the NPE on the " " string, not the " 2030" String.~Tim
SomeoneElsea at 2007-7-9 0:14:15 > top of Java-index,Java Essentials,Java Programming...
# 2

What I want " 2030" to show is 002030, there are 2 blanks in the string " 2030". It should mean 20 minutes 30 seconds.

If I remove the empty date then also I get an NPE.

The question is, when a format like "12030" results in 012030, a format like " 2030" [2 blanks] should result in 002030, but it actually results in NPE. So the HH pattern parsing is incorrect.

Is this string format " 2030" [2 blanks] correct?

The documentation says H should have a value from 0-23, but does not talk about blanks.

250884a at 2007-7-9 0:14:15 > top of Java-index,Java Essentials,Java Programming...
# 3

>>The question is, when a format like "12030" results in 012030, a format like " 2030" [2 blanks] should result in 002030, but it actually results in NPE. So the HH pattern parsing is incorrect.

Actually, When I parse that string, I get 020300 for the result, and the NPE is thrown when it attempts to parse " ". What happens is taht a hour has to be a number from 0-23, so it prses the String until it hits a digit, in this case the 2, then parses the next 2 digits (03) as the minutes, then the 0 as seconds.

I am a bit surprised that this works even when setLenient(false) is called.

The reaon " " throws an exception is that there are no digits to parse.

~Tim

SomeoneElsea at 2007-7-9 0:14:15 > top of Java-index,Java Essentials,Java Programming...
# 4
The easy fix is to call thedatestring.trim() before trying to parse it.
Dick_Adamsa at 2007-7-9 0:14:15 > top of Java-index,Java Essentials,Java Programming...