Java 如何修改日期格式?

Java 如何修改日期格式?,java,android,Java,Android,请帮我找到解决这个问题的办法 这是我从Json对象获得的日期格式 Sun Jan 08 00:00:00 GMT+05:30 2017. 我需要将此日期格式转换为2017-01-05 我实现了一些类似这样的代码 android.text.format.DateFormat df = new android.text.format.DateFormat(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"

请帮我找到解决这个问题的办法 这是我从Json对象获得的日期格式

Sun Jan 08 00:00:00 GMT+05:30 2017.
我需要将此日期格式转换为
2017-01-05

我实现了一些类似这样的代码

android.text.format.DateFormat df = new android.text.format.DateFormat();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = simpleDateFormat.parse(summary1.getDeliveryDate());
if(date1.equals(df.format("yyyy-MM-dd", new java.util.Date())))
{
       spinnerArray.add("Cancel");
} 

我无法进入循环。。。请帮助我找到解决方案

示例代码

    String inputDate = "2017-01-05 00:00:00";
    System.out.println("Input Date: "+inputDate);
    String pattern = "yyyy-MM-dd";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    Date formatDate = simpleDateFormat.parse(inputDate);
    String outputDate = simpleDateFormat.format(formatDate);
    System.out.println("Output Date: " +outputDate);
输出:

Input Date: 2017-01-05 00:00:00
Output Date: 2017-01-05

请检查并让我知道。

也许这对你有用

Date date1 = summary1.getDeliveryDate();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
if(fmt.format(date1).equals(fmt.format(new Date())))
{
    spinnerArray.add("Cancel");
} 

当我这样写“summary1.getDeliverydate()”时,请参考这个。我得到了一个类似“2017-01-05 00:00:00.0”的日期格式。我想消除这个“00:00:00.0”。请将summary1.getDeliverydate()存储到一个变量中,检查变量的数据类型,然后应用上述逻辑。summary1.getDeliverydate()它返回字符串数据类型。。我使用了您的代码字符串模式=“yyy-mm-dd”;SimpleDataFormat SimpleDataFormat=新的SimpleDataFormat(模式);字符串formatDate=SimpleDataFormat.parse(日期);在这个代码执行之后,我得到了这样的日期格式:Sun Jan 08 00:00:00 GMT+2017年5:30,但我需要这样的2017-01-05检查上面的代码。。。执行“Date formatDate=simpleDateFormat.parse(Date)”代码后,请使用“String outputDate=simpleDateFormat.format(formatDate);”我已经在上面提到了代码。请用这个。你会得到欲望的输出。我也在eclipse中进行了测试;日期格式日期=SimpleDataFormat.parse(inputDate);字符串outputDate=SimpleDataFormat.format(formatDate);