java 如何将“Thu Aug 04 00:00:00 IST 2011”解析为“04-08-2011”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6952758/
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 18:00:20  来源:igfitidea点击:

How to parse "Thu Aug 04 00:00:00 IST 2011" to "04-08-2011"?

javadate-format

提问by n92

I want to parse the date Thu Aug 04 00:00:00 IST 2011to dd-MM-YYformat like 04-08-2011. How to do this in Java?

我想分析的日期Thu Aug 04 00:00:00 IST 2011,以dd-MM-YY类似格式04-08-2011。如何在 Java 中做到这一点?

回答by Bozho

Use the following format to parse: EEE MMM dd HH:mm:ss z yyyywith SimpleDateFormat.parse(..)

使用以下格式解析:EEE MMM dd HH:mm:ss z yyyywithSimpleDateFormat.parse(..)

The use another SimpleDateFormatwith the dd-MM-yyformat, to format(..)the resultant date. Something like:

在使用其他SimpleDateFormatdd-MM-yy格式,向format(..)得到的日期。就像是:

SimpleDateFormat parseFormat = 
    new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = parseFormat.parse(dateString);
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yy");
String result = format.format(date);

回答by Thennisha

I hope you can find the solution from the sample code below

我希望你能从下面的示例代码中找到解决方案

import java.text.DateFormat;
import java.util.Date;

public class DateFormatExample
{

    public static void main(String args[])
    {

        Date now=new Date();
        System.out.println("dd/mm/yy format:" +DateFormat.getDateInstance(DateFormat.SHORT).format(now));

    }
}

Now you will get your required format....

现在您将获得所需的格式....