• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Why hh:mm:ss format can be used to parse HH:mm:ss ?

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


***
timestamp=2018-07-27 18:56:32.72,  ts.toString()=2018-07-27 18:56:32.72
convertTime = 2018-07-27 18:56:32.072

Question:  apparently timestamp "ts" is in format like HH:mm:ss instead of hh:mm:ss.  But when I use "hh:mm:ss" to format it, it still works.  Why ??
 
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Mutansan wrote:. . . it still works. . . .

No, it doesn't. Why are you getting 32.72 in one place and 32.072 elsewhere?
 
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a bit of a head scratcher. There are two issues here:
1) You are not "formatting" with your SimpleDateFormat, you are using it to parse. So if you declare a time format with "hh" why do you get 24hr time instead of 12hr time? This seems like a logical thing for the parser to do when presented with a number >= 12. So, not surprised.
2) Why do you get fractions of a second, one case as ".29" and in the other case ".029"? This I think is a bug in the Timestamp#toString() method. Technically Timestamp doesn't keep milliseconds but instead keeps seconds plus nanoseconds. And, of course, util.Date is only accurate to milliseconds. -- Upon further tests: sometimes I get ".29" and sometimes I get ".029" from toString(). Go figure.
 
Carey Brown
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
UPDATE: I no longer blame Timestamp#toString(). It appears that when you parse a date/time string using SimpleDateFormat and you specify s.SSS it expects EXACTLY three digits after the decimal point. This is also true if you specify s.SS or s.S. When you supply less than 3 digits it pads it with zeros ON THE LEFT instead of on the right. Timestamp#toString() will sometimes return less than 3 digits.
 
Campbell Ritchie
Marshal
Posts: 70234
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . it pads it with zeros ON THE LEFT instead of on the right.

How strange. No, it isn't if you are going to print the milliseconds as a number on their own.

Timestamp#toString() will sometimes return less than 3 digits. . . .

Presumably if the last digit would have been a zero. Another twist to Winston's warning against using Strings. The toString() method can here produce a String in the wrong format.
 
Carey Brown
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The documentation on Timestamp#toString() says that the default formatting is
Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.fffffffff, where fffffffff indicates nanoseconds.
First off, this format uses 'mm' for both the month and the minutes, which is wrong.
It uses 'ss.ffff' to indicate seconds and fraction thereof, which is different than 'ss.SSS' used in SimpleDateFormat which specifies seconds and milliseconds. SDF does not support specifying 'ss.fff'.
 
Jack Mutansan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, I think I see your point about the 2 digit and 3 digit milli/nano seconds part.  Thanks.  Let me ask about the HH and hh --- Are you saying when I use SimpleDateFormat to parse a Timestamp, it actually doesn't matter if I use "yyyy-MM-dd HH:mm:ss.SSS" or "yyyy-MM-dd hh:mm:ss.SSS" as format to parse ?  Again, please ignore the SSS or SS part, just care the HH and hh for now. Thanks.
 
Jack Mutansan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, you wrote
"You are not "formatting" with your SimpleDateFormat, you are using it to parse. So if you declare a time format with "hh" why do you get 24hr time instead of 12hr time? This seems like a logical thing for the parser to do when presented with a number >= 12. So, not surprised. "

-- I know "hh" is 0-12 with AM/PM and I know Timestamp is 0-24 in hours.  So I intentionally use "hh" to parse timestamp when it is in the PM, to see what happens.  I couldn't understand why it parsed OK.  Could you explain little more  ?  Thanks.
 
Carey Brown
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When presenting hours that are >= 12 to a parser that is expecting it to be less than 12, the designer of the parser had two options: throw an exception, or presume the hours were given in 24hr format. They chose the latter.
 
Saloon Keeper
Posts: 22483
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:The documentation on Timestamp#toString() says that the default formatting is
Formats a timestamp in JDBC timestamp escape format. yyyy-mm-dd hh:mm:ss.fffffffff, where fffffffff indicates nanoseconds.
First off, this format uses 'mm' for both the month and the minutes, which is wrong.
It uses 'ss.ffff' to indicate seconds and fraction thereof, which is different than 'ss.SSS' used in SimpleDateFormat which specifies seconds and milliseconds. SDF does not support specifying 'ss.fff'.



I think you're confusing the template with the abstract format. We say "hh:mm:ss mm/dd/yy" (in the US) to indicate the format of a date when discussing it, but of course that's too ambiguous for code, hence the MM versus mm format styles. Could have been "{hr}:{min}:{sec} {month}/{day}/{year}" if the class designers had been in a different mood.

One thing to bear in mind is that in almost all cases in Java times are held in a millisecond or microsecond value field without regard to things like AM/PM. So when parsing a time like "15:37" you have 2 choices: forgive it and convert it or throw an exception. Although "15:37 AM" would be an affront.
 
Jack Mutansan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, wait a min.  I just found something interesting.  hh:mm:ss does not always tolerate a HH:mm:ss when parsing it.  It tolerates it and behaves well in most cases.  But today when I tested it when it was 12:xx:xxSSS ( when it was just aftern noon) it gave problem.  It shows 00:xx:xx.SSS instead of 12:xx:xx.SSS.  So it seems it was confused when the hour is 12, I didn't test how it behaves when it was just after midnight.  So, i guess it is safer to use HH:mm:ss.SSS to parse a timestamp and can't fully trust hh:mm:ss.    Please correct me if you think I am wrong.
 
Carey Brown
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think part of your confusion is that you are not clear about when the issues is with SimpleDateFormat#format() or SiimpleDateFormat#parse(). If you parse with "hh" and give it '12' hours it will create the correct number of milliseconds. However, when you use "hh" to reformat it you will get '00' for the hours.
 
Jack Mutansan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I think part of your confusion is that you are not clear about when the issues is with SimpleDateFormatter#format() or SiimpleDateFormatter#parse(). If you parse with "hh" and give it '12' hours it will create the correct number of milliseconds. However, when you use "hh" to reformat it you will get '00' for the hours.



Got it.  I should use HH to format it if I want to see it in 0-24 form, given  it is a timestamp.  But for parsing the timestamp string using hh or HH doesn't make difference.
 
Carey Brown
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. I sort of take that back. Seems like '12' for "hh" does get interpreted as '12' midnight (am). However '13' for "hh" gets interpreted as '01' PM.

I  find it best, when trying to puzzle out some of these issues, to create a simple program to play with and demonstrate the issue. Something like this:
This keeps you and those who are trying to help you on the same page.

In conclusion: If your data is in 12hr format use "hh". If it's in 24hr format then use "HH".
 
Carey Brown
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jack Mutansan wrote:Got it.  I should use HH to format it if I want to see it in 0-24 form, given  it is a timestamp.  But for parsing the timestamp string using hh or HH doesn't make difference.

In my last post I found that HH vs hh DOES make a difference. You were correct. It chokes if you give it '12' hours. Best not to assume that the creator of SimpleDateFormat can cover all the cases where you use HH or hh incorrectly.

Q: Do you really need to do all this manipulation using Strings? Could you just rely on the Timestamp or Date internal numeric values?
 
Jack Mutansan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic