Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Java Patterns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Patterns" Watch "Java Patterns" New topic
Author

Java Patterns

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
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

Joined: Jul 26, 2007
Posts: 10170
    
    8

Check out Simple Date Format


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Oct 26, 2007
Posts: 88
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

Joined: Jul 26, 2007
Posts: 10170
    
    8

Recommended reading http://java.sun.com/docs/books/tutorial/uiswing/components/spinner.html , especially the formatting part
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

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.


SCJP 6 || SCWCD 5
vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
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

Joined: Jun 17, 2009
Posts: 273

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

Joined: Jan 17, 2006
Posts: 1296
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.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 13, 2005
Posts: 38033
    
  22
I concede defeat.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
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.

Joanne
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What doesn't appear? I see AM/PM in your posts :)
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Patterns
 
Similar Threads
Please review my code
help with program
reusable Pattern object
how to extract two numbers from a string
java Date questions