javascript Groovy UTC 日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16975404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Groovy UTC date formatting
提问by Cornelius Qualley
Okay, so I'm doing a simple UTC date formatting script which takes UTC and converts it in to a specific format for date and time. I can get the script to work fine in javascript, but the plugin I'm using requires Groovy apparently. I'm not familiar with it, I've been told that the programming is essentially the same as it can use javascript libraries/language. In any case, here is my code snippet:
好的,所以我正在做一个简单的 UTC 日期格式化脚本,它采用 UTC 并将其转换为特定的日期和时间格式。我可以让脚本在 javascript 中正常工作,但我使用的插件显然需要 Groovy。我不熟悉它,有人告诉我编程本质上与它可以使用 javascript 库/语言相同。无论如何,这是我的代码片段:
import java.util.*;
var d = new Date();
var CurrentDay = ('0' + d.getUTCDate()).slice(-2);
var CurrentMonth = ('0' + d.getUTCMonth()).slice(-2);
var CurrentYear = d.getUTCFullYear();
var CurrentHours = ('0' + d.getUTCHours()).slice(-2);
var CurrentMinutes = ('0' + d.getUTCMinutes()).slice(-2);
var CurrentSeconds = ('0' + d.getUTCSeconds()).slice(-2);
var DateFormatted = CurrentYear.toString() + CurrentMonth.toString() + CurrentDay.toString() + CurrentHours.toString() + CurrentMinutes.toString() + CurrentSeconds.toString();
return DateFormatted;
I'm getting this error message back when I attempt to run my script:
当我尝试运行我的脚本时,我收到此错误消息:
groovy.lang.MissingMethodException: No signature of method: Script1.var() is applicable for argument types: (java.util.Date) values: [Thu Jun 06 21:18:43 CDT 2013]
Possible solutions: wait(), run(), run(), every(), any(), wait(long)
Parameters:
{}
Any help would be much appreciated. Also, I can get this script to run exactly as I would like as a normal javascript.
任何帮助将非常感激。此外,我可以让这个脚本完全按照我想要的普通 javascript 运行。
回答by dmahapatro
The equivalent of var
from JavaScript is "def"in Groovy. Moreover, setting up a date format is easier in Groovy using Groovy Date:
在 Groovy 中,相当于var
from JavaScript 是“def”。此外,使用Groovy Date在 Groovy 中设置日期格式更容易:
TimeZone.setDefault(TimeZone.getTimeZone('UTC'))
def now = new Date()
println now
println now.format("MM/dd/yyyy'T'HH:mm:ss.SSS'Z'")
//Prints:
Fri Jun 07 02:57:58 UTC 2013
06/07/2013T02:57:58.737Z
You can modify the format according to your need. For available formats refer SimpleDateFormat.
您可以根据需要修改格式。有关可用格式,请参阅SimpleDateFormat。
回答by Basil Bourque
tl;dr
tl;博士
Java syntax here, but you can call java.time classes in Groovy too.
此处为 Java 语法,但您也可以在 Groovy 中调用 java.time 类。
Instant.now()
.toString()
2017-01-23T01:23:45.987654321Z
2017-01-23T01:23:45.987654321Z
Avoid legacy classes
避免遗留类
The Question and other Answers use troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
问题和其他答案使用麻烦的旧日期时间类,这些类现在是遗留的,被 java.time 类取代。
Using java.time
使用 java.time
Apparently you want to generate a String in standard ISO 8601format representing a date-time value in UTCtime zone.
显然,您想以标准ISO 8601格式生成一个字符串,表示UTC时区中的日期时间值。
The java.time.Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
本java.time.Instant
类表示UTC时间线与纳秒的分辨率上一会儿。
Instant instant = Instant.now() ;
The java.time classes use ISO 8691 formats by default when parsing/generating strings.
java.time 类在解析/生成字符串时默认使用 ISO 8691 格式。
String output = instant.toString() ;
2017-01-23T01:23:45.987654321Z
2017-01-23T01:23:45.987654321Z
If you want whole seconds, without a fraction, truncate.
如果您想要整秒,没有分数,请截断。
Instant instant = Instant.now().truncatedTo( ChronoUnit.SECONDS ) ;
回答by Learner
You should generally never change the default time zone just for your local benefits, as that affects all the code in JVM - it may want the actual local time zone to be the default, for example. Hence, using TimeZone.setDefault() is a bad practice unless you are sure that all your code will want that time zone and that all code is yours and that this will never change. That is a lot to ask.
通常,您不应仅仅为了本地利益而更改默认时区,因为这会影响 JVM 中的所有代码 - 例如,它可能希望将实际本地时区设为默认时区。因此,使用 TimeZone.setDefault() 是一种不好的做法,除非您确定您的所有代码都需要该时区,并且所有代码都是您的并且永远不会改变。要问的太多了。
Instead you should explicitly specify the time zone of interest in your calls.
相反,您应该在调用中明确指定感兴趣的时区。
If you want to use a date/time format that is specific to the locale of the user/machine, you should use the DateFormatfactory, specifically one of the following static methods - they will take care of complex locale/language specifics not possible using any other approach (including SimpleDateFormatter):
getDateTimeInstance() - for the default locale and default styles.
- getDateTimeInstance(int dateStyle, int timeStyle)for the default locale and specified styles
- getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)for specified styles and locale
如果您想使用特定于用户/机器语言环境的日期/时间格式,您应该使用DateFormat工厂,特别是以下静态方法之一 - 它们将处理无法使用的复杂语言环境/语言细节任何其他方法(包括 SimpleDateFormatter):
- getDateTimeInstance(int dateStyle, int timeStyle)用于默认语言环境和指定样式
- getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)指定样式和语言环境
Once you get that DateFormatter instance you will want to call the setTimeZone(TimeZone.getTimeZone('UTC'))on it. That will make that formatter use that time zone. You can also use any other time zone.
获得 DateFormatter 实例后,您将希望在其上调用setTimeZone(TimeZone.getTimeZone('UTC'))。这将使该格式化程序使用该时区。您也可以使用任何其他时区。
Finally, you will call the dateFormat.format(Date date)method to get a String representation of that date, in the locale, style and time zone of choice.
最后,您将调用dateFormat.format(Date date)方法以获取该日期的字符串表示形式,采用所选的语言环境、样式和时区。
If you do NOT want locale specific format but want to fix to something of your own, such as ISO, instead of creating the DateFormat instance using a static factory method described above, you will want to use the SimpleDateFormatinstead. Specifically you will use the new SimpleDateFormat(String pattern)constructor. The pattern would be following the definitions in the SimpleDateFormat JavaDoc, and could be 'yyyy-MM-dd HH:mm:ss.SSS', for example.
如果您不想要特定于语言环境的格式,但想要修复您自己的格式,例如 ISO,而不是使用上述静态工厂方法创建 DateFormat 实例,您将需要使用SimpleDateFormat来代替。具体来说,您将使用新的 SimpleDateFormat(String pattern)构造函数。该模式将遵循 SimpleDateFormat JavaDoc 中的定义,例如可以是“yyyy-MM-dd HH:mm:ss.SSS”。
You would then call the setTimeZone() method on that SimpleDateFormat instance the same was as for (any) DateFormat. In Groovy you can skip the explicit SimpleDateFormat creation and you can instead use the Groovy Date enhancement, its format(String format, TimeZone tz)method - it will do the SimpleDateFormat stuff under the covers.
然后,您将在该 SimpleDateFormat 实例上调用 setTimeZone() 方法,就像(任何)DateFormat 一样。在 Groovy 中,您可以跳过显式 SimpleDateFormat 创建,而可以使用 Groovy Date 增强,它的format(String format, TimeZone tz)方法 - 它将在幕后执行 SimpleDateFormat 内容。
Locale-specific format approach example (Java and/or Groovy):
特定于语言环境的格式方法示例(Java 和/或 Groovy):
String gimmeUTC(Date date, Locale locale) {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(date);
}
Fixed format approach example (Java and/or Groovy):
固定格式方法示例(Java 和/或 Groovy):
String gimmeUTC(Date date, Locale locale) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(date);
}
Fixed format example (Groovy only):
固定格式示例(仅限 Groovy):
String gimmeUTC(Date date) {
return date.format('yyyy-MM-dd HH:mm:ss.SSS', TimeZone.getTimeZone('UTC'));
}