• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Patterns

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Group,

I am working on a project in which i need to date date and time values from input text.

I am planning to use Pattern class for this.

my questions are
- how feasible is to use this class for this operation
- Is there any other option i can look forward for.

For more example i made this pattern to read simple time input



although it is working well. but this is not all the case. if user enters 06:30a, or 06:30aM, then i am screwed.

what should i do for this. What will be the best approach for this project.



 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since aM is not a recognised abbreviation (it should always be am not AM) you should be throwing an Exception for erroneous input.
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Check out Simple Date Format

ya i looked in to this class too; But i am not able to get any clear idea about how should i proceed.

Please help me out.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay shanker wrote:
Maneesh Godbole wrote:Check out Simple Date Format

ya i looked in to this class too; But i am not able to get any clear idea about how should i proceed.

Please help me out.


You use the SimpleDateFormat class to take a String and convert it into a Date object. The Date object holds time information too, not just date information. Once you have your Date object, you can use the Calendar class to extract the time information out of it.
 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something missing in my requirement and your suggestions.

1. i am not aware of what format user enters to input time values. It can be like i said 9:30PM or 9:30 PM or 9:30 pm or 9:30pm or 9:30p.
2. i see SimpleDateFormat supports to fetch time formats from any input text. but not able to identify how to set variable format like i have given above.
3. Throwing exception is not my motive. I want to perceive what any user has given in the input. and i want it to be correctively perceived,

 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vijay shanker wrote:i am not aware of what format user enters to input time values.

As with any input you get from the user, you should (1) clean it so that it matches the format you're looking for, (2) force the user to input it in a specific way, or (3) use a combination of both. Maybe something like this:
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Since aM is not a recognised abbreviation (it should always be am not AM) you should be throwing an Exception for erroneous input.

I have not seen a reference that agrees with this. That must be unique to the UK (or is it U.K. ). http://en.wikipedia.org/wiki/12-hour_clock I bet you also put your curly braces in the wrong place don't you.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've never seen capitals for am or pm. But the 12-hours clock is now largely restricted to informal uses.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see capitals for AM/PM all the time, and most people are informal and use a 12-hour clock, at least in the US, if they're not in the military.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I concede defeat.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javaranch uses PM in the posting times at the top of each individual message.
I think there may be a bug though because it doesn't change to AM when I post a message in the mornings.

Edit - just noticed it only appears in other people's posts - not in my posts.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What doesn't appear? I see AM/PM in your posts :)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's strange. The PM didn't appear in my post when I viewed the thread straight after posting, but it does appear now.
Edit - ignore that. i wasn't logged in when I saw the PM in my post. D'oh.

Anyway, enough of the serious stuff. Which of my posts do you see AM in ?

Edit2 - apologies to vijay for hijacking his post. Probably should stop this now as I don't think he has got an answer yet.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic