Guest User

Untitled

a guest
Oct 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. //endDate is 31.10.2018
  2. def format = messageService.getMessage(code: 'default.date.short.format')
  3.  
  4. //--> dd.MM.yyyy for DE and MM/dd/yy for EN
  5. println("Use format: " + format)
  6.  
  7. SimpleDateFormat sdf = new SimpleDateFormat(format);
  8.  
  9. //set timezone (America/New_York)
  10. sdf.setTimeZone(TimeZone.getTimeZone(user.timeZoneID))
  11.  
  12. //parse endDate
  13. Date parsedEndDate = sdf.parse(endDate)
  14.  
  15. //create a calendar instance (e.g. America/New_York)
  16. Calendar calendarEnd = Calendar.getInstance(TimeZone.getTimeZone(user.timeZoneID));
  17.  
  18. //set time
  19. calendarEnd.setTime(parsedEndDate);
  20.  
  21. //set hour/minute automatically
  22. calendarEnd.set(Calendar.HOUR_OF_DAY, 23)
  23. calendarEnd.set(Calendar.MINUTE, 59)
  24.  
  25. //at this point it should be 31.10.2018, 23:59 (german format, timezone America/New_York)
  26.  
  27. //Convert to UTC before saving date in DB (MySQL)
  28. calendarEnd.setTimeZone(TimeZone.getTimeZone('UTC'))
  29.  
  30. //save the date
  31. def obj = new Foo(date:calendarEnd).save(flush:true)
  32.  
  33. <g:formatDate
  34. timeZone="${user.timeZoneID}"
  35. date="${fooInstance?.calendarEnd}"
  36. format="${message(code: 'default.date.format', default: 'MM/dd/yyyy, hh:mm a')}"/>
Add Comment
Please, Sign In to add comment