-2

I have a date :

String endDate = "2015-07-22 00:00:00";

I construct a formatter for this date :

DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

But how to convert date to format yyyy-MM-dd? I want to get only 2015-07-22.

Tim
  • 41,901
  • 18
  • 127
  • 145
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119

2 Answers2

0

This is way simpler than you'd think.

You can just use

new SimpleDateFormat("yyyy-MM-dd");

It ignores everything that doesn't fit the format.

Tim
  • 41,901
  • 18
  • 127
  • 145
0
 public static void main(String[] args) throws ParseException {
        String date = "2015-07-22 00:00:00";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = dateFormat.parse(date);
        String newDateString = dateFormat.format(date1);
        System.out.println(newDateString);
    }

output

2015-07-22
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116