Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes date format in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date format in java" Watch "date format in java" New topic
Author

date format in java

jashaswini jagadeb
Greenhorn

Joined: Oct 15, 2009
Posts: 1
hi...
i want to get the date from textField of any format in swing. (i.e.dd/mm/yyyy or dd.mm.yyyy or dd-mm-yyyy) .please reffer the code.
please help me.
thanks in advance.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

jashaswini jagadeb wrote:hi...
i want to get the date from textField of any format in swing. (i.e.dd/mm/yyyy or dd.mm.yyyy or dd-mm-yyyy) .please reffer the code.
please help me.
thanks in advance.


Hi jashaswini, this is NotACodeMill, so we won't just give you code to use, but we can help you with your problem. Have a look at the DateFormat, and its implementing SimpleDateFormat child classes. They provide tools for parsing many of the common date formats - with the ability to pass in your own custom format String. You won't be able to use a single format to parse 'any format', you will have to provide a specific set of formats that are recognized.

Take a look at those classes and try out some code. If you have problems come on back and let us know and we can help you work through them.


Steve
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
And welcome to JavaRanch
Vikash Ananda
Ranch Hand

Joined: Oct 12, 2009
Posts: 32
Hi jashaswini jagadeb,

There are many samples that you can google that gives you a button on pressing them you can see the calender where specific dates can be clicked which you can trap. You can use these dates for your application. Please find something similar. This will eliminate users to make mistakes.

Thanks,
Vikash Anand.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19725
    
  20

If you want to try parsing using a DateFormat without any exceptions thrown, use the parse method that takes a ParsePosition:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
Consider Paul's rocket mass heater.
 
subject: date format in java