Skip to content

Instantly share code, notes, and snippets.

@nightspotlight
Last active June 22, 2018 20:41
Show Gist options
  • Save nightspotlight/b873c0a733b9a49103f88a8689bf8f39 to your computer and use it in GitHub Desktop.
Save nightspotlight/b873c0a733b9a49103f88a8689bf8f39 to your computer and use it in GitHub Desktop.
A Groovy script to display given time in different timezones
#!/usr/bin/env groovy
//TimeZone.setDefault(TimeZone.getTimeZone('Europe/Warsaw'))
if (args.size() == 0 || !args.find { it ==~ /^\d{2}\.\d{2}\.\d{4}\ \d{2}\:\d{2}$/ }) {
String thisScript = new File(this.getClass().protectionDomain.codeSource.location.path).getName()
System.err.println "Error parsing command line arguments!"
System.err.println "Usage: ${thisScript} <dateTime>"
System.err.println "Accepted dateTime format is dd.mm.yyyy hh:mm"
System.err.println "Example: ${thisScript} '25.05.2018 05:00'"
System.exit(1)
}
else {
String inputDateTime = args[0]
// https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
String inputDateTimeFormat = 'dd.MM.yyyy HH:mm'
String outputDateTimeFormat = 'EEEE, d MMMM yyyy, HH:mm z'
Date dateTime = Date.parse(inputDateTimeFormat, inputDateTime)
println dateTime.format(outputDateTimeFormat, TimeZone.getTimeZone('America/Los_Angeles'))
println dateTime.format(outputDateTimeFormat, TimeZone.getTimeZone('America/Chicago'))
println dateTime.format(outputDateTimeFormat, TimeZone.getTimeZone('Europe/Warsaw'))
println dateTime.format(outputDateTimeFormat, TimeZone.getTimeZone('Europe/Moscow'))
println dateTime.format(outputDateTimeFormat, TimeZone.getTimeZone('Asia/Kolkata'))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment