Groovy UTC Date Formatting

Ok, so I am doing a simple UTC date formatting script that accepts UTC and converts it to a specific format for date and time. I can get the script to work fine in javascript, but the plugin that I'm using obviously needs Groovy. I am not familiar with this, I was told that programming is essentially the same as for using javascript libraries / language. Anyway, here is my code snippet:

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 get this error message when I try 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 greatly appreciated. In addition, I can get this script to run just as I would like as regular javascript.

+4
source share
3 answers

The equivalent of var from JavaScript is "def" in Groovy. What's more, setting the date format is easier in Groovy with Groovy Date :

 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 change the format to suit your needs. For available formats, see SimpleDateFormat .

+13
source

TL; DR

Java syntax is here, but you can also call java.time classes in Groovy.

 Instant.now() .toString() 

2017-01-23T01: 23: 45.987654321Z

Avoid obsolete classes

The question and other answers use the nasty old time classes, which are now deprecated, being superseded by the java.time classes.

Using java.time

Apparently you want to generate a string in the standard ISO 8601 format representing the date value in the UTC time zone .

The java.time.Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

 Instant instant = Instant.now() ; 

The java.time classes use the ISO 8691 formats by default when parsing / generating strings.

 String output = instant.toString() ; 

2017-01-23T01: 23: 45.987654321Z

If you want whole seconds, without a beat, crop.

 Instant instant = Instant.now().truncatedTo( ChronoUnit.SECONDS ) ; 
+1
source

As a rule, you never change the default time zone only for your local advantages, since this affects all the code in the JVM - perhaps it wants the actual local time zone to be the default. Therefore, using TimeZone.setDefault () is bad practice if you are not sure that all of your code will want this time zone and that all of your code is yours and that this will never change. This is a lot to ask.

Instead, you should explicitly specify the time zone of interest for your calls.

Once you get an instance of DateFormatter, you'll want to call setTimeZone (TimeZone.getTimeZone ('UTC')) . This will make this formatter use this time zone. You can also use any other time zone.

Finally, you call the dateFormat.format (Date date) method to get a string representation of that date in the locale, style, and time zone of the selection.

If you DO NOT want a locale-specific format, but want to fix something of your own, such as ISO, instead of creating an instance of DateFormat using the static factory method described above, you will want to use SimpleDateFormat . In particular, you will use the constructor of the new SimpleDateFormat (String pattern) template . The template will follow the definitions in the JavaDoc SimpleDateFormat and may be, for example, "yyyy-MM-dd HH: mm: ss.SSS".

Then you call the setTimeZone () method for the SimpleDateFormat instance the same as for (any) DateFormat. In Groovy, you can skip the explicit creation of SimpleDateFormat, and instead you can use the Groovy Date extension, its format (String format, TimeZone tz) - this will do the SimpleDateFormat material under the covers.

An example approach to a specific format format (Java and / or 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); } 

An example of a fixed format approach (Java and / or 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):

 String gimmeUTC(Date date) { return date.format('yyyy-MM-dd HH:mm:ss.SSS', TimeZone.getTimeZone('UTC')); } 
0
source

Source: https://habr.com/ru/post/1484951/


All Articles