[Logo]
Forums Register Login
SimpleDateFormat parsing arguments


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

instead of throwing error.


Please explain why you think the code should throw an error.
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.
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.
 

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.
 

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.
 

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.
 

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"
 

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.

"
 

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.
 

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.
 

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
As the documentation for parse says, it starts from the beginning.
 

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.

 

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.
 

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!”
  • F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
    Why should you try IntelliJ IDEA ?
    https://coderanch.com/wiki/696337/IntelliJ-IDEA


    This thread has been viewed 230 times.

    All times above are in ranch (not your local) time.
    The current ranch time is
    Aug 16, 2018 16:26:33.