简体   繁体   中英

Convert string to date in a specific format without time

I have a doubt that when I am converting a String to date then It will return it as:

Sun Apr 30 00:00:00 GMT+05:30 2017

but I want the format to be as:

2017-04-30

Date last_date_date = new SimpleDateFormat("yyyy-MM-dd").parse("2017-04-30");

So Which method I need to apply for that?

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

//current date into your required format
String str = dateFormat.format(new Date());
DateFormat df = SimpleDateFormat("yyyy-MM-dd");
Date lastDateDate = df.parse("2017-04-30");


String date = df.format(lastDateDate);

Your code is working well, here is a proof:

Date last_date_date = new SimpleDateFormat("yyyy-MM-dd").parse("2017-04-30");
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(last_date_date));

OUTPUT:

2017-04-30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM