Handsken
Handsken

Reputation: 674

Can I parse a date to fit a String Array?

Im having a String array in my constructor which is supposed to read a line in a .txt document by using a bufferedreader. It is containing Strings int´s and two dates. My structure looks like this:

Private String Name;
Private int Number;
Private Date BDate;

String[] splitsarray = line.split("%");     
this.Name= splitsarray [0];  
this.Number= Integer.parseInt(splitsarray [1]);

I wan't to do someting like this.BDate= splitsarray [2] but i can't figuer out how to make it work.

Any hints or tips for me?

Upvotes: 1

Views: 115

Answers (2)

Mick Sear
Mick Sear

Reputation: 1566

You need to look at the JDK text format classes: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Or alternatively, look at using something like Joda Time, which is an external dependency, but has a rich API.

An example:

String date = "2011-11-05T09:00:00";
java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
java.util.Date d = fmt.parse(date);
System.out.println(d);
//Sat Nov 05 09:00:00 GMT 2011

Upvotes: 0

Prince John Wesley
Prince John Wesley

Reputation: 63698

Pass the right date pattern in the SimpleDateFormat constructor argument.

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
this.BDate = formatter.parse(splitsarray [2]); 

Upvotes: 2

Related Questions