I need help to format the date in the code that is below. Should be the date shown on the form: 05. May 2014
. Please give me suggestions how to do it.
package person;
public class Person {
public static void main(String[] split) {
String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow";
String[] newText = text.split("[./ ]");
for(int i=0; i {
String name = newText[i].split(" ")[0];
String lastName = newText[i+1].split(" ")[0];
String dateOfBirth = newText[i+2].split(" ")[0];
String placeOfBirth = newText[i+3].split(" ")[0];
System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + dateOfBirth + ", place of birth: " + placeOfBirth);
}
}
}
Answer
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
public class Format {
public static void main(String[] args) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("dd ',' MMM yyyy", Locale.ENGLISH);
SimpleDateFormat parser = new SimpleDateFormat("ddMMyyyy");
String text = "John.Davidson/05051988/Belgrade Michael.Barton/01011968/Krakov Ivan.Perkinson/23051986/Moscow";
String[] newText = text.split("[./ ]");
for(int i=0; i {
String name = newText[i].split(" ")[0];
String lastName = newText[i+1].split(" ")[0];
String dateOfBirth = newText[i+2].split(" ")[0];
String placeOfBirth = newText[i+3].split(" ")[0];
System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + formatter.format(parser.parse(dateOfBirth)) + ", place of birth: " + placeOfBirth);
}}
}
No comments:
Post a Comment