开发者

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

I want to parse the date Thu Au开发者_如何学Cg 04 00:00:00 IST 2011 to dd-MM-YY format like 04-08-2011. How to do this in Java?


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

The use another SimpleDateFormat with the dd-MM-yy format, to format(..) the resultant date. Something like:

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);


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....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜