开发者

How to convert date?

I'am using the calendar of primefaces:

<p:calendar  value="#{TestAjax.date1}" showOn="button" />

this calandar return the date with this format:

Fri Jul 08 00:00:00 GMT+01:00 2011

How can I convert this date to this format : 2011-07-08 or 2011/07/08

Thank you

UPADTE

I have converted the date ,below the code I hope this will help sombody else

DateFormat out = new SimpleDateForm开发者_如何学编程at("MM/dd/yyyy");
String date=out.format(myDate1);


You can use SimpleDateFormat or Joda-Time's DateTimeFormat to parse it into a date object and then use the same class also to format it to the other format.


Let me hope following example helps you.

import java.util.Date;  
import java.text.SimpleDateFormat;  

public class test  
{  
    public static void main( String[] args )  
    {  
        String myDatePattern1 = "yyyy-MM-dd";  
        String myDatePattern2 = "yyyy/MM/dd";  
        SimpleDateFormat df = new SimpleDateFormat( myDatePattern1 );  

        Date today = new Date();
        System.out.println( "Date today: " + today );
        System.out.println( "Date in pattern 1: " + df.format( today ) );

        df.applyPattern( myDatePattern2 );
        System.out.println( "Date in pattern 2: " + df.format( today ) );
    } // psvm(...)
} // class test

Output of this example would be as below:
Date today: Sat Jul 09 16:01:23 IST 2011
Date in pattern 1: 2011-07-09
Date in pattern 2: 2011/07/09


Use the following:

<p:calendar value="#{TestAjax.date1}" pattern="yyyy-dd-MM" />

Pattern attribute is used to change pattern of calendars.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜