• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check whether name of the File is in a Specifi format??

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am expecting the Filename to be in a specified format that is "Test_INT.NO1.<dd><MM><yy><HH><mm><ss>.txt"

for example

"Test_INT.NO1. 28 06 10 12 25 56.txt"

But Please let me know how do i check the File name has got a specific format or not ??

Please share your ideas on this .

Thanks in advance .
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a regular expression?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like Paul's answer.
 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Filenames retrieved will be in the form of String. In my opinion, identifying the time format accurately is not always possible.

The only way I see is, to validate every parameter for its limits. such as 31 for <dd>, 12 for <MM> etc. This validation technique could help you to identify some parameters and factor the rest.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd prefer to use a DateFormat to check if the date is valid, rather than manually checking bounds. Using a DateFormat and its parse(String, ParsePosition) method you can even prevent any ParseExceptions:
That parse method returns null if the input is invalid. The other check also makes sure that the parsing consumes the entire input; parse methods usually allow trailing data; for instance, with "Test_INT.NO1. 28 06 10 12 25 56.txtabc" as input, the parse position's index will point at the a, allowing you to parse some more. Since we don't want to parse more or allow this trailing data the check ensures that the format will consume the entire text.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic