Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

apache flex - Date to String <-> String to Date

I get a Date of my database and I need to show it as a String. So in Flex I do this:

public static function dateToString(cDate:Date):String {
        return cDate.date.toString()+"."+
            cDate.month.toString()+"."+
            cDate.fullYear.toString()+" "+
            cDate.hours.toString()+":"+
            cDate.minutes.toString()+":"+
            cDate.seconds.toString();
}

But I get for example the result:

13.7.2010 0:0:15

How can I fill the day, month, hours, minutes, seconds with padded 0?

And, I go back from String to Date with:

DateField.stringToDate(myTextInput.text, "DD.MM.YYYY HH:MM:SS");

Is this correct? I want to have a Date which I will transfer via BlazeDS to a J2EE Backend, but I only see in the database then a null value. So something is going wrong...

Best Regards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have you seen the DateFormatter class?

Example:

import mx.formatters.DateFormatter;

private var dateFormatter:DateFormatter;

private function init():void
{
    dateFormatter = new DateFormatter();
    dateFormatter.formatString = 'DD.MM.YYYY HH:NN:SS'
}

public function dateToString(d:Date):String
{
    return dateFormatter.format(d);
}

public function stringToDate(s:String):Date
{
    return dateFormatter.parseDateString(s);
}

It looks like somebody was asleep the day the wrote Flex 3.2, because DateFormatter::parseDateString is a protected function. It looks like they fixed that by 3.5.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...