How to convert and check date with different format in Groovy - date

In my groovy code, I have 2 Dates
def date1 = "2021-08-02T03:22:12Z"
dev date2 = new Date()
my goal is, if date1 is 5 days older than date2 return true.,
date2 looks like that Thu Aug 05 10:13:56 UTC 2021
but How to convert 2 dates with right date format before comparing?
any solutions?

def date1 = Date.parse("yyyy-MM-dd'T'HH:mm:ss","2021-08-02T03:22:12Z")
def date2 = new Date()
def daysDiff = date2-date1
println daysDiff

Related

ISO8601dateformatter 1970-01-01 00:00:00 issue

How to deal with the new year and ISO8601 returning last year as year component.
To my horror, I realized ISO8601DateFormatter was returning 1977 as a year to the 1978-01-01 00:00:00
It took a while to realize this. That turned out is not wrong. Nonetheless, given the specific year of 1978, for the formatted to return 1977 is shocking.
I don't even need the timestamp. How can I reliably retrieve the specified year without having to add a second to every calendar date?
import Foundation
let datestring = "1978/1/1"
var formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.formatOptions = [.withFullDate]
let date2 = formatter.date(from: datestring) ?? Date()
print(date2)
var calendar = Calendar(identifier: .iso8601)
var year = calendar.component(.year, from: date2)
var month = calendar.component(.month, from: date2)
var day = calendar.component(.day, from: date2)
var era = calendar.component(.era, from: date2)
print("year \(year) month \(month) day \(day) era: \(era)")
===
1978-01-01 00:00:00 +0000
year 1977 month 12 day 31 era: 1
By default the Calendar instance will have your local timeZone. You can see this by printing print(calendar.timeZone.abbreviation() ?? "UNKNOWN"). In my case (in Seattle, WA, USA) it prints "PDT". If you simply set your calendar timezone to UTC it prints exactly what you expect:
year 1978 month 1 day 1 era: 1

Can I compare two dates in Smalltalk?

I have two dates which I need to compare, if one is past second. That means:
date1 := Date newDay: 10 month: 12 year: 2017
date2 := Date newDay: 1 month: 1 year: 2020
So in this case date2 is past date1, so I need this to be true.
But
date1 := Date newDay: 10 month: 12 year: 2017
date2 := Date newDay: 3 month: 7 year: 2015
should return false.
Anyone got hints? Appreciate!
I guess this depends on which dialect you are using, but ANSI standard already defines < for DateAndTime which seems similar to Date. I tried your code in Pharo and Dolphin and date1 < date2 works just fine for your needs (even if Date instantiation in Dolphin is a little bit different).

Convert from Unix Timestamp to date in Groovy

I have a date in unix timestamp and I want to convert it to human readable...
def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"
This gives me..
clean date 49064-02-13
where I was expecting "2017-02-03".
What am I doing wrong?
I even casted the timestamp to a long explicitly as suggested in this answer.
To convert Unix time Timestamp to Java Date:
def timestamp = 1486146877214 // milliseconds
def date = new Date( timestamp ).toString()
assert 'Fri Feb 03 13:34:37 EST 2017' == date.toString() // EN/US date format
Create timestamps and/or confirm dates at TimestampConvert.net or EpochConverter.com, etc.
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs

Issue saving a string as ISO date format

I'm trying to save a date to MongoDB from FullCalendar in my Grails application.
I'm trying to parse the string 2015-12-27T00:00:00.000Z into the below format:
def startDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.start)
def endDate = new Date().parse("YYYY-MM-dd'T'HH:mm:ss.SSSXXX",it.end)
But, weirdly when I print the formatted date, I get Sun Dec 28 05:30:00 IST 2014. I don't know what or how that particular date is picked.
You should use lowercase y for year. Uppercase Y is for "Week year".
new Date().parse("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", "2015-12-27T00:00:00.000Z")
===> Sat Dec 26 19:00:00 EST 2015
import java.text.SimpleDateFormat;
println new SimpleDateFormat("yyyy-MM-dd HH:mm:ssX").parse("2018-07-30 09:57:15 +0800")

Groovy Date code producing unexpected output

Why is below code producing output like this ?
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-mm-dd hh:mm:ss', oldDate )
Output : Sat Jan 05 01:34:54 EST 2013
When it should simply produce December 5 as the date in the output ? In fact in the original string whatever month I put in it produces the exact same output.
The problem is your date format isn't correct. mm is for minutes where MM is for month. So it should be:
String oldDate = "2013-12-05 01:34:54.270"
Date date = Date.parse( 'yyyy-MM-dd hh:mm:ss', oldDate )
You can find out more from the Java API docs for SimpleDateFormat.