• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

error in formatting miliseconds for date objects

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently you ask for three digits for milliseconds in your date format. Try asking for two.
[ April 05, 2005: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wont work either:

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

 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried one S, two Ss and three Ss. none of them works.

is this a JRE problem?

thanks, pete
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
System.out.println(convertedDate);

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

- Jeff
 
pete johnson
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Carol,

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
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pete,

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
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

Regards,
Pete
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic