【问题标题】:Groovy DateTime Format unexpected outputGroovy DateTime Format 意外输出
【发布时间】:2018-11-12 14:15:13
【问题描述】:

下面的代码没有返回预期的输出。它有什么问题?

def a = new Test(new Date());

println a.getLogFormatDate()

class Test
{
    String _dateTime;
    static final String _logDateFormat = "E MMM dd HH:mm:ss zzz yyyy";
    static final String _timeZoneUTC = "UTC";

    Test(Date dateTime)
    {
        _dateTime = dateTime;
    }

    public String getLogFormatDate()
     {
        return _dateTime.format(_logDateFormat, TimeZone.getTimeZone(_timeZoneUTC));
     }
}​

预期输出:

2018 年 11 月 12 日星期一 14:10:46 UTC

实际输出:

E MMM dd HH:mm:ss zzz yyyy

【问题讨论】:

    标签: datetime groovy


    【解决方案1】:

    您已经定义了String 类型的_dateTime 类字段,因此您的getLogFormatDate 调用

    String.format("E MMM dd HH:mm:ss zzz yyyy", TimeZone.getTimeZone("UTC"))
    

    在你期待的时候

    Date.format("E MMM dd HH:mm:ss zzz yyyy", TimeZone.getTimeZone("UTC"))
    

    被调用。

    _dateTime 字段定义为Date,它将按您的预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2013-05-30
      • 1970-01-01
      • 2019-05-16
      • 2013-02-12
      相关资源
      最近更新 更多