日期到字符串 <->字符串到日期 [英] Date to String <-> String to Date

查看:42
本文介绍了日期到字符串 <->字符串到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了数据库的日期,我需要将其显示为字符串.所以在 Flex 我这样做:

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

但我得到例如结果:

13.7.2010 0:0:15

13.7.2010 0:0:15

如何用填充的 0 填充日、月、小时、分钟、秒?

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

这是正确的吗?我想要一个日期,我将通过 BlazeDS 将其传输到 J2EE 后端,但我只在数据库中看到一个空值.所以出了点问题...

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

此致.

推荐答案

你看过 DateFormatter 类?

示例:

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

看起来在编写 Flex 3.2 的那天有人睡着了,因为 DateFormatter::parseDateString 是一个受保护的函数.看起来他们在 3.5 之前修复了这个问题.

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.

这篇关于日期到字符串 <->字符串到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆