Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat parsing arguments  RSS feed

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


This program gives output 22:33, instead of throwing error. Am I doing anything wrong?
 
Rancher
Posts: 2862
32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

instead of throwing error.


Please explain why you think the code should throw an error.
 
Marshal
Posts: 58336
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the documentation for that method say? You are actually passing a valid time at the beginning of that text.
I think the wrong thing is using Date and DateFormat in the first place. Date is a dreadful class and it has been superseded by much better things which you can read about in the Java™ Tutorials.
 
vipul John
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My SimpleDateFormat pattern is HH:mm, but I gave input as '22:33 JavaRanch'. This is incorrect.
'HH:mm' is 24-hour format. Suppose, If i give '22:30 AM' as input, SimpleDateFormat should throw ParseException.
 
vipul John
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What does the documentation for that method say? You are actually passing a valid time at the beginning of that text.
I think the wrong thing is using Date and DateFormat in the first place. Date is a dreadful class and it has been superseded by much better things which you can read about in the Java™ Tutorials.



I can not use Java 8 API time package, as one of the other projects has a dependency of Java 7. So, that is a lot of work , approvals and integration testing.
 
Campbell Ritchie
Marshal
Posts: 58336
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vipul John wrote:. . . . I can not use Java 8 API time package, as one of the other projects has a dependency of Java 7. . . .

What a pity. The details of the parse() method appear not to have changed between Java7 and Java8.
 
vipul John
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

vipul John wrote:. . . . I can not use Java 8 API time package, as one of the other projects has a dependency of Java 7. . . .

What a pity. The details of the parse() method appear not to have changed between Java7 and Java8.



What do you mean? My question is why parse() accepting extra input in the pattern? Is this functionality expected? I have set lenient to false. so that's strictly HH:mm should be accepted right.
 
Ranch Hand
Posts: 38
Eclipse IDE Fedora MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vipul John wrote:

This program gives output 22:33, instead of throwing error. Am I doing anything wrong?



Can you post a result where your input is of following format :- "SomeText... HH:MM"
 
Rancher
Posts: 3385
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vipul John wrote:
What do you mean? My question is why parse() accepting extra input in the pattern? Is this functionality expected? I have set lenient to false. so that's strictly HH:mm should be accepted right.



Quoting from the documentation linked to by Campbell earlier, for the parse() method:
"
Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

"
 
Campbell Ritchie
Marshal
Posts: 58336
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vipul John wrote:. . . What do you mean?

I mean that it is unfortunate that you are constrained to use poor quality legacy code when much better code is available.

My question is why parse() accepting extra input in the pattern? Is this functionality expected? . . .

Did you actually look at the documentation? As Dave Tolls points out, it is all in there.
 
Campbell Ritchie
Marshal
Posts: 58336
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mishra Saurabh wrote:. . . . where your input is of following format :- "SomeText... HH:MM"

Please go through the documentation and find out what HH:MM means.
 
Mishra Saurabh
Ranch Hand
Posts: 38
Eclipse IDE Fedora MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mishra Saurabh wrote:. . . . where your input is of following format :- "SomeText... HH:MM"

Please go through the documentation and find out what HH:MM means.



I do know what that means. I just wanted to make sure whether the method was designed to look for time in HH:MM format anywhere in the whole string or whether it is strictly looking for HH:MM at the start.
I apologize if I am not able to make myself much clearer
 
Dave Tolls
Rancher
Posts: 3385
38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the documentation for parse says, it starts from the beginning.
 
vipul John
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

vipul John wrote:. . . What do you mean?

I mean that it is unfortunate that you are constrained to use poor quality legacy code when much better code is available.

My question is why parse() accepting extra input in the pattern? Is this functionality expected? . . .

Did you actually look at the documentation? As Dave Tolls points out, it is all in there.



Migration to Java 1.8 is a major change. It requires lot of effort, so most probably in couple of months we might work on it.

 
vipul John
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

vipul John wrote:
What do you mean? My question is why parse() accepting extra input in the pattern? Is this functionality expected? I have set lenient to false. so that's strictly HH:mm should be accepted right.



Quoting from the documentation linked to by Campbell earlier, for the parse() method:
"
Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

"



My requirement is, My project uses 24 hour format pattern. It works until one of the tester using the application specified time as 22:30 AM format which it still accepts and does not throw a validation error. After specifying the format I can write anything, it does not matter.
Should I handle this using string.split method separated by space. If the array contains more than one string, then throw a validation message saying "specify proper time format". Can It be handled in any other way, without writing much code.

It is unfortunate to write code in this way, but most probably this will be temporary once project is migrated to java 1.8 we start using LocalDate and LocalTime.
 
Campbell Ritchie
Marshal
Posts: 58336
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

vipul John wrote:. . . Should I handle this using string.split method separated by space. . . . .

No. It works now. That's it.

Unless of course, the remainder of the String is of significance. In that case you might want to record the remainder of the String and substring... split... or similar would help you find the remainder of the String. But it is beginning to look as if you had three independent problems:
  • 1: How to get your times; that seems solved, so it is more of a non‑problem.
  • 2: How to sanitise your text inputs. That should probably be done before you try parsing the times.
  • 3: How to port your system to Java8 without breaching the principle, “If it ain't broke, don't fix it!”
  •  
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!