Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SimpleDateFormat does not handle leading blanks for HHmmss format

Status
Not open for further replies.

25884

Programmer
Mar 12, 2001
46
AP
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" ?
 
It shows the output for 2030 with 1.4 and 1.5 for me and shows the error when parsing the empty date.

Cheers,
Dian
 
Why not just trim() your date string before passing it to the formatter ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hi,

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.


Thanks,
Sonali
 
If a two spaces always means it should be two zeros, then :

String s = " 002030";
s = s.replaceAll(" ", "0");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I don't think you need to do that, it works for me as is now.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top