java 将字符串转换为没有时间的特定格式的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10024632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:18:52  来源:igfitidea点击:

Convert string to date in a specific format without time

javaandroid

提问by Kanika

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

2017 年 4 月 30 日星期日 00:00:00 GMT+05:30

but I want the format to be as:

但我希望格式为:

2017-04-30

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?

那么我需要申请哪种方法呢?

采纳答案by Eng.Fouad

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

回答by waqaslam

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

//current date into your required format
String str = dateFormat.format(new Date());

回答by pepyakin

DateFormat df = SimpleDateFormat("yyyy-MM-dd");
Date lastDateDate = df.parse("2017-04-30");


String date = df.format(lastDateDate);