K
K
Kirill Morozov2016-06-26 16:55:35
Java
Kirill Morozov, 2016-06-26 16:55:35

How to properly format date using SimpleDateFormat in Java?

Tell me, please, where is the problem? For some reason, the date is not formatted...
We need this output:
Ivanov Ivan Ivanovich 31 12 1987
Vasya 15 5 2013
And it turns out like this:
Ivanov Ivan Ivanovich Sat Jan 31 00:12:00 MSK 1987
Vasya Tue Jan 15 00:05:00 MSK 2013

public class Solution {
    public static final List<Person> PEOPLE = new ArrayList<Person>();

    public static void main(String[] args) throws IOException, ParseException
    {
        BufferedReader br = new BufferedReader(new FileReader(args[0]));
        String line;

        while((line = br.readLine()) != null) {

            String string = line.replaceAll("[a-zA-Zа-яА-Я]+", "").trim();

            SimpleDateFormat format = new SimpleDateFormat("dd mm yyyy");
            Date date = format.parse(string);

            String name = line.replaceAll("[\\d]+", "");
            PEOPLE.add(new Person(name, date));
        }

        for (int i = 0; i < PEOPLE.size(); i++)
        {
            System.out.println(PEOPLE.get(i).getName() + " " + PEOPLE.get(i).getBirthday());
        }
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScratchBoom, 2016-06-26
@grifxdesign

mm is minutes
MM is months
+ date is not formatted anywhere in the code, it is only parsed.
You need to output dateFormat.format(date), not just date (birthday in code)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question