解析 – 使用TimeZone重新格式化字符串日期
我对Groovy很新(但已经爱上了它).我不是新编码,但到目前为止还没有多少经验.
我在做什么? 我将字符串日期保存到var odate = 'Wed Oct 31 00:00:00 CET 2012' 我需要将此Date重新格式化为类似的东西
我试图将日期解析为mentioned in another question,但我得到的只是一个例外. String odate = 'Wed Oct 31 00:00:00 CET 2012' def parsedodate = new Date().parse('E MMM dd H:m:s z yyyy',odate) println parsedodate.format('MM/dd/yyyy h:m:s')
经过一番阅读和一些试验后,错误我发现,解析方法似乎只能解释德国日期.手动将字符串日期更改为德语格式(我就是这样)后,以下工作. String odate = 'Mi Okt 31 00:00:00 2012' //Mi = Wednesday,Okt = October,removed timezone def parsedodate = new Date().parse('E MMM dd H:m:s yyyy',odate) // removed the z println parsedodate .format('MM/dd/yyyy h:m:s') 但是,我需要解析器接受英文日期格式. 解决方法
整个groovy解决您的问题的方法是:
import java.text.SimpleDateFormat odate="Wed Oct 31 00:00:00 CET 2012" englishPattern="E MMM dd H:m:s z yyyy" SimpleDateFormat englishDateFormat = new SimpleDateFormat( englishPattern,Locale.ENGLISH); //SimpleDateFormat germanDateFormat = new SimpleDateFormat( germanPattern,Locale.GERMAN); Date englishDate = englishDateFormat.parse( odate ); //Date germanDate = germanDateFormat.parse( odate ); String englishOutput = englishDate .format( englishPattern ); //String germanOutput = germanDate .format( germanPattern ); englishDate.format("MM/dd/yyyy hh:mm:ss") (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |