Raphael MM Raphael MM - 1 year ago 59
Java Question

get only year month and day of a string yyyy-mm-DDThh:mm:ssZ

Sorry for the simple question, but I researched a lot here in the forum and none of the above helped me. I have a date in String format "yyyy-mm-ddThh: mm: ssZ" exactly this format in my database. I need to catch only the year, month and day to make a comparison with another string.
The closest I got was that it does not work.

String dateArracoamento = arracoamento.getDate();

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
SimpleDateFormat output = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(dateArracoamento);
String dateArracoamentoFormat = output.format(d);

For example: My dateArracoamento = 2016-04-28T10:10:10Z. I need to get only 2016-04-28 and not the complete date. How can I do this?

Answer Source

You could be able to parse that date string using the same formatter you're using to format the output.

    String input = "2016-04-28T10:10:10Z";
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    String output = dateFormat.format(dateFormat.parse(input));