The moose likes Java in General and the fly likes error in formatting miliseconds for date objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "error in formatting miliseconds for date objects " Watch "error in formatting miliseconds for date objects " New topic

error in formatting miliseconds for date objects

pete johnson

Joined: Mar 24, 2005
Posts: 19
hi, i have a java.util.Date object and i am using SimpleDateFormat
to format it as follows:

Date myDate = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
System.out.println(myDate); (1)
System.out.println(sdf.format(myDate)); (2)

however, if a Date object in line 1 is displayed as 04/05/2005 04:55:23.14,
then the second is displayed as 04/05/2005 04:55:23.140 (an extra zero).

this is a puzzle to me. how can i make the second display the same as the
first one?

thanks, pete
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Currently you ask for three digits for milliseconds in your date format. Try asking for two.
[ April 05, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281

wont work either:
Tue Apr 05 13:50:24 BST 2005
04/05/2005 13:50:24.343

java amateur
pete johnson

Joined: Mar 24, 2005
Posts: 19
i tried one S, two Ss and three Ss. none of them works.

is this a JRE problem?

thanks, pete
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
It does pay attention to the number of S's in the format String, it just doesn't work quite like you were thinking. It's printing milliseconds, not a decimal value (like we interpret it because of the . there). So, it drops zeroes off the front not the back.

I printed out a bunch of dates with 1, 2, and 3 S.

[ April 05, 2005: Message edited by: Carol Enderlin ]
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Two things here:

First, the "S" represents milliseconds. Milliseconds will always be accurate to three digits after the second's decimal point because, well, they're milliseconds... thousandths of a second.

What you seem to be looking for is, um... I guess they'd be called "centiseconds". The SimpleDateFormat object does not have a pattern string for that, as far as I know.

Second, for numbers, the letters in a formatting pattern string represent the number of minimum digits, not the number of maximum digits.

So, how would you get just two digits after the seconds? There are probably many different ways to do it. The first way that pops into my head is to use "SSS" (so you always get three digits), then take the resulting String and just lop off the last character:

String convertedDate = sdf.format(myDate);
convertedDate = convertedDate.substring(1, convertedDate.length());

That might do the trick, although it technically won't round off the milliseconds to centiseconds.

- Jeff
pete johnson

Joined: Mar 24, 2005
Posts: 19

Thanks for your input. How did you get 04/05/2005 14:23:48.009 in the
first place? I wish my program could do the same thing.

If I reverse the order of your following tests,
S: 04/05/2005 14:23:48.9
SS: 04/05/2005 14:23:48.09
SSS: 04/05/2005 14:23:48.009

I will get the followinig, no matter 1 S or 2 Ss or 3 Ss I use:
S: 04/05/2005 14:23:48.900
SS: 04/05/2005 14:23:48.900
SSS: 04/05/2005 14:23:48.900

This clearly shows a problem: the system is not consistent.

Jeff, I very much appreciate your input.

"Milliseconds will always be accurate to three digits.. "

I dont quite get it. 04/05/2005 14:23:48.900 and 04/05/2005 14:23:48.9
should be interpreted differently, right?

Your convertedDate trick probably does not always work. I may well have
a date with a milliseconds being 900. If you remove the last zero, you
get 90 and that is different than it is actual value 900. Doing a trick
to get accurate milliseconds as a date object has when it is printed
directly via System.out.print(new Date()), in my opinion, shows either
the design for SimpleDateFormat misses something or
probably we dont know the right way to do it.

Thanks, Pete
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364

In a loop I got a new Date, then printed it out with the different formats. Scan the list by eyeball to find ones that are affected by the format string. I printed out 100 or so different dates.

You do not seem to be getting the point that S/SS/SSS will display differently for 9 (009), but not 900. 8 (008), but not 800. It's not working like you wanted it to, but it seems to be working consistently.

[ April 05, 2005: Message edited by: Carol Enderlin ]
pete johnson

Joined: Mar 24, 2005
Posts: 19
Carol, thanks very much for your input. It really helps.
you are right! I ran 3 rounds of your code and got one
in the 3rd round:

ate: Tue Apr 05 23:24:52 EDT 2005
SSS: 04/05/2005 23:24:52.093
SS: 04/05/2005 23:24:52.93
S: 04/05/2005 23:24:52.93

I think I made a mistake of translating another problem into
this generic one. My orginial problem is: my web app grabs
data from a SQL server and the following is part of the code:

dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
Date date = (Date)columnValue;
System.out.println("date:" + date);
if (date != null) {
String d = dateFormat.format(date);
System.out.println("Formated: " + d);

The following is one date pulled from the database:

date:2005-04-03 16:05:28.11
Formated: 04/03/2005 16:05:28.110

I thought it was a generic Java problem. Looks like I need to
find out why this happened in my specific environment.
I did another test as follows:

public static void test2() throws Exception {

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date myDate = sdf1.parse("2005-04-03 16:05:28.11");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.S");
System.out.println("Date: " + sdf1.format(myDate));
System.out.println("SSS: " + sdf2.format(myDate));
System.out.println("S: " + sdf3.format(myDate));

and I still got the corrects:

Date: 2005-04-03 16:05:28.011
SSS: 04/03/2005 16:05:28.011
S: 04/03/2005 16:05:28.11

Thanks very much for your help here!

I agree. Here's the link:
subject: error in formatting miliseconds for date objects
It's not a secret anymore!