解析字符串到目前为止,但结果是不同的格式 [英] Parse String to date, but result is different format

查看:121
本文介绍了解析字符串到目前为止,但结果是不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将一个字符串解析为日期,但解析后,格式会发生变化。我有我的控制器:

  def dates = params.id 
def dates2 = new Date()。parse (yyyyy:MM:dd,日期)
System.out.println(dates2)



<但是结果是Tue Oct 23 00:00:00 CST 2012,而不是我添加的格式。我怎么能强制它的格式yyyy:MM:dd?

解决方案

这只是Java日期的字符串表示形式

如果你使用 Date.format 打印出来,你会看到你想要的格式:

  def dates = params.id 

//解析是静态的,不需要新的
def dates2 = Date.parse('yyyy:MM:dd',日期)

println(dates2.format('yyyy:MM:dd'))

(我也假设你的意思是 yyyy ,而不是5 y s)

I have been trying to parse a string into dates, but after parsing, the format changes. I have this in my controller:

def dates = params.id
def dates2 = new Date().parse("yyyyy:MM:dd", dates)
System.out.println(dates2)

But the result is Tue Oct 23 00:00:00 CST 2012 instead of the the format that I added. How can I force it to that format yyyy:MM:dd?

解决方案

That's just the String representation of a Java Date

If you use Date.format to print it out, you'll see the format you want:

def dates = params.id

// parse is static, no need for new
def dates2 = Date.parse( 'yyyy:MM:dd', dates )

println( dates2.format( 'yyyy:MM:dd' ) )

(I also assume you mean yyyy, not 5 ys)

这篇关于解析字符串到目前为止,但结果是不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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