I am using Grails 2.0.3 (groovy 1.8.6) with joda-time:1.3.1 and joda-time-templates plugins.
Everything works perfectly but I would like to change displayed format for date and time.
How can I do that? Every domain is scaffolded so I do not have access to any view to render it manually.
My domain
import org.joda.time.*
import org.joda.time.contrib.hibernate.*
class Game {
Team host
Team guest
String location
DateTime date
static mapping = {
date type: PersistentDateTimeTZ, {
column name: "date_timestamp"
column name: "date_zone"
}
}
}
rendered date
Date 5/24/12 5:53 PM
I would like to get it as
Date 5 may 2012 17:53 PM
Is there any way to translate name of month
define this in config.groovy
jodatime { format.org.joda.time.DateTime = "dd MMM YYYY HH:mm a" }
and that should take care of it.
Related
I am trying to parse the following date in Jenkins 2021-10-14T18:12:20.578+00:00 however, I am getting the error Unparseable date: "2020-01-01T10:10:20.578+00:00"
This is my code, not sure what I am doing wrong:
Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578+00:00");
EDIT:
Thanks to Kaus, I found that my date is not formatted properly and should be 2020-01-01T10:10:20.578GMT+00:00
I'm getting this date from some other files. I can replace + with GMT+ as follow:
def myDate = "2020-01-01T10:10:20.578+00:00"
myDate = myDate.replaceAll("\\+", "GMT\\+")
How can I do the same thing if my date is "2020-01-01T10:10:20.578-06:00"
The following is replacing every "-"
def myDate = "2020-01-01T10:10:20.578-06:00"
myDate = myDate.replaceAll("\\+", "GMT\\+").replaceAll("\\-", "GMT\\-")
Output: "2020GMT-01GMT-01T10:10:20.578GMT-06:00"
Use X for ISO8601 time zone, instead of Z for RFC 822 time zone.
(from https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html)
import java.text.SimpleDateFormat
Date myDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX")
.parse("2020-01-01T10:10:20.578+00:00")
Missing GMT there
Date myDate= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").parse("2020-01-01T10:10:20.578GMT+00:00");
I have a Grails 2.2.4 project, and I'm trying to write a unit test for a method that queries over lastUpdated, like so:
Tile.createCriteria().list {
lt('lastUpdated', new Date() - 1)
}
This method works fine in real life, but fails in my unit tests because I can't create any test data with lastUpdated other than now. Setting myTile.lastUpdated explicitly doesn't work, since that's an update and thus triggers the auto-timestamping. Turning off auto timestamping requires the eventTriggeringInterceptor, which doesn't seem to be available in unit tests. Mocking the default Date constructor to return other values was also no help. Direct SQL updates are not available in unit tests at all.
Is this possible in unit tests at all, or do I have to write an integration test?
It's interesting that you say mocking the default date constructor to return other values is no help. I successfully do that quite often when I have queries such as yours that new up a date.
For your situation, I would have a unit test that looked something like this:
def 'test lastUpdated query'() {
setup:
Title lessThan = new Title(lastUpdated:new Date(1477152000000)) //22 Oct 2016 16:00 UTC, should be found
Title equalTo = new Title(lastUpdated:new Date(1477238400000)) //24 Oct 2016 16:00 UTC, should not find, not less than 1 day before, but equal to 1 day before
Title notLessThan = new Title(lastUpdated:new Date(1477296000000)) //24 Oct 2016 08:00 UTC, should not find, not less than 1 day before
Date date = new Date(1477324800000) //24 Oct 2016 16:00 UTC
Date.metaClass.constructor = {-> return date}
when:
List<Title> result = service.someMethod()
then:
result.size() == 1
result.contains(lessThan)
!result.contains(equalTo)
!result.contains(notLessThan)
}
One of my co-workers has written an application in Grails 2.4.4 (I know, it's dated). One problem the app has is that you can enter a date like 2/31/2015 and it will be accepted as valid and will show up in your domain object as 3/3/2015 instead.
Is there any easy way to prevent this from happening using grails? Or do we instead have to rely on client side validation for this particular property?
Thanks.
Assuming that Grails is using DateFormat to parse the date String, the issue is that it's using a lenient Calendar. For example, with lenient set to true (the default), the result is as you described:
import java.text.SimpleDateFormat
def sdf = new SimpleDateFormat('MM/dd/y')
assert sdf.parse('02/31/2015').toString() == 'Tue Mar 03 00:00:00 EST 2015'
But, if you change it to false, you'll get an exception for the same date:
sdf.lenient = false
try {
sdf.parse('02/31/2015').toString() == 'Tue Mar 03 00:00:00 EST 2015'
} catch (java.text.ParseException ex) {
// java.text.ParseException: Unparseable date: "02/31/2015"
println 'Oops'
}
So to handle this validation in your controller, you can
Create a command object.
Have the command object accept the date as a String rather than a Date, and perform the date validation.
Modify the controller action to use the command object instead of params. This may require modifying the GSP code as well.
I have a problem within a grails 2.3 application, when it comes to data binding and correct date formats.
I use a datepicker (jQuery ui) that provides a <input type="hidden" /> that holds the selected date in ISO_8601 format. It will post a value like this: 2015-08-14 to the controller. The form itself and the post result is correct.
I use this simplified model:
class Thing {
DateTime lastUpdated
static constraints = {
lastUpdated nullable: true
}
}
when I try to create an entity, I will face this error message:
Invalid format: "2015-08-14" is malformed at "15-08-14"
A first research lead me to this change in the Config.groovy:
jodatime.format.html5 = true
(Link 3 in the list below)
Appying this leads to change. Now the error message is:
Invalid format: "2015-08-14" is too short (flip table)
Another try was to change the databinding.dateFormats to this (also in the Config.groovy):
grails.databinding.dateFormats = [ "yyyy-MM-dd HH:mm:ss.S","yyyy-MM-dd'T'hh:mm:ss'Z'", "yyyy-MM-dd"]
Which has no effect what so ever.
For my understanding a given date format should automatically be marshaled in a dateTime object. What configuration did I miss?
Here are relative question, that sadly did not help me:
bind date to command object in Grails
GORM default Date format when sending date to Grails
Grails unable to unmarshall date/time from JSON back into joda DateTime
You should add next line in config.groovy
jodatime { format.org.joda.time.DateTime = "yyyy-MM-dd" }
But if you don't need time in this field, it's better to use LocalDate instead of DateTime here.
class Thing {
LocalDate lastUpdated;
...
jodatime {
format.org.joda.time.DateTime = "yyyy-MM-dd HH:mm:ss"
format.org.joda.time.LocalDate = "yyyy-MM-dd"
}
So you will use DateTime where you need date with time and LocalDate where date is enough
In my domain I have
Time startTime
Time endTime
In my controller I need to covert the time from the view which is in a format of HH:MM to the acceptable format to submit to the domain. I have installed the plugin Joda-Time but I've come a bit stuck.
def startTime = params.startTime
def fmt_in = DateTimeFormat.forPattern("HH:mm:ss")
def fmt_out = ISODateTimeFormat.dateTime()
println fmt_out.print(fmt_in.parseDateTime(startTime))
sorry, newbie to groovy grails
After parsing for DateTime, you need to transform it to the desired type. For date and time without considering timezone I suggest you to use LocalDateTime and LocalTime.
def formatter = DateTimeFormat.forPattern("HH:mm:ss")
LocalTime time = formatter.parseLocalDateTime(params.startTime).toLocalTime()