[英]format date in groovy and save in desired format with milliseconds
I'm trying to get a full date with milliseconds. 我正在尝试以毫秒为单位的完整日期。
I tried following 我尝试了以下
Date callTime = new Date();
String formattedDate = callTime.format('MM/dd/yyyy HH:mm:ss.SSS');
println "formattedDate : " + formattedDate;
def saveThisDate = new Date().parse("MM/dd/yyyy HH:mm:ss.SSS", formattedDate);
println "saveThisDate : " + saveThisDate;
And it gave output as following 它给出了如下输出
formattedDate : 12/22/2014 15:51:47.427
saveThisDate : Mon Dec 22 15:51:47 IST 2014
where the formattedDate
gives correct output but I've to save it in a date so i tried doing saveThisDate
. 在哪里formattedDate
提供正确的输出,但我必须将其保存在一个日期,所以我尝试做saveThisDate
。 But it doesn't save milliseconds. 但这并不能节省毫秒。
Where I'm doing wrong? 我在哪里做错了?
Dates are just dates, they don't have a format. 日期只是日期,没有格式。 You only give a data type a format when you convert it to a String. 当您将数据类型转换为字符串时,才给它一种格式。 Create a String from the Date and give it the format you need. 从日期创建一个字符串,并为其指定所需的格式。
It saves, You need to format it as well: 它可以保存,您还需要对其进行格式化:
Date callTime = new Date();
String formattedDate = callTime.format('MM/dd/yyyy HH:mm:ss.SSS');
println "formattedDate : " + formattedDate;
def saveThisDate = new Date().parse("MM/dd/yyyy HH:mm:ss.SSS", formattedDate);
println "saveThisDate : " + saveThisDate.format('MM/dd/yyyy HH:mm:ss.SSS');
if you don't do additional formatting, the saveThisDate
is printed out in default date format (depends on locale of your machine) and it doesn't include ms. 如果您不进行其他格式化,则saveThisDate
将以默认日期格式(取决于您计算机的语言环境)打印出来,并且不包含ms。
follow Opal's example 跟随蛋白石的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.