Groovy字符串数据到日期 [英] Groovy String data to Date
本文介绍了Groovy字符串数据到日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将字符串转换为日期?
我试过这个:
@RequestMapping(/ data / {data})
字符串buscar(@PathVariable字符串数据,模型模型){
model.addAttribute'dataBuscar',数据
def newDate = data
def df1 = new SimpleDateFormat(dd / MM / yyyy 00:00:00)
data = df1.parse(newDate)
model.addAttribute'acessos',acessoService.buscar(data)
'acesso / acesso.index'
}
但显示此消息:
无法解析日期:12-01-2014
为什么?任何想法 ? 您的 SimpleDateFormat
模式与您尝试使用的字符串不匹配 如果你需要解析一个字符串如12-01-2014,你需要:
new SimpleDateFormat(dd-MM-yyyy);
测试过的Groovy脚本:
import java.text.SimpleDateFormat
def format = new SimpleDateFormat(dd-MM-yyyy)
def date = format.parse(14-01 -2014)
println date //打印2014年1月14日星期二00:00:00 CST 2014
正如@tim_yates所指出的,Groovy提供了方便的方法:
Date.parse('dd-MM-yyyy ',14-01-2014)
How to convert String to Date ?
I tried this:
@RequestMapping("/data/{data}")
String buscar(@PathVariable String data, Model model) {
model.addAttribute 'dataBuscar', data
def newDate = data
def df1 = new SimpleDateFormat("dd/MM/yyyy 00:00:00")
data = df1.parse(newDate)
model.addAttribute 'acessos', acessoService.buscar(data)
'acesso/acesso.index'
}
but show me this message:
Unparseable date: "12-01-2014"
Why? Any idea ?
解决方案
Your SimpleDateFormat
pattern does not match the string you're trying to parse.
If you need to parse a string like "12-01-2014", you need:
new SimpleDateFormat("dd-MM-yyyy");
Tested Groovy script:
import java.text.SimpleDateFormat
def format = new SimpleDateFormat("dd-MM-yyyy")
def date = format.parse("14-01-2014")
println date // prints "Tue Jan 14 00:00:00 CST 2014"
As @tim_yates points out, Groovy provides the convenient method:
Date.parse('dd-MM-yyyy', "14-01-2014")
这篇关于Groovy字符串数据到日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文