Thursday, 10 May 2018

Java - Formatting the date printing




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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...