0

I have been struggling with this even after doing so much of research on such a simple thing, so I need some help here.

I need to pass current date in date data type only in 'yyyy-MM-dd' format. SimpleDateFormat converts current date to string type and while trying to parse though it gets converted to Date type but changes the format.

I need currentDate in the format 'yyyy-MM-dd' of Date Type.

if(!session.dtfromDate && !session.dttoDate)
                            eq("startDate", currentDate)
tim_yates
  • 167,322
  • 27
  • 342
  • 338
Isha
  • 95
  • 1
  • 11
  • what error are you getting? Perhaps posting your java code snippet might help to answer your question. – Abubakkar Apr 27 '15 at 06:25
  • What you want is not possible. `Date` objects do not have a format, so "a `Date` object in the format 'yyyy-MM-dd'" makes no sense. A `Date` object is just a date value, just like an `int` is just a number - it doesn't have a format by itself. – Jesper Apr 27 '15 at 06:34
  • this is about grails/gorm and how to build this up for a query (and not java/groovy in general), right? – cfrick Apr 27 '15 at 06:38

1 Answers1

-1

I have managed to figure out a solutions to this. First got my desired format which obviously converted it to a String and then parsed this to a Date. It has worked perfectly fine.

SimpleDateFormat nsdf = new SimpleDateFormat('yyyy-MM-dd')
String currentDate = new SimpleDateFormat('yyyy-MM-dd').format(new Date());
Date newDate = (Date)nsdf.parse(currentDate)

if(!session.dtfromDate && !session.dttoDate)

                    eq("startDate", newDate)
Isha
  • 95
  • 1
  • 11
  • 1
    unless i miss some bigger picture, this could be shorted to `def newDate = new Date()` – cfrick Apr 27 '15 at 07:40
  • That would give the current date yes, but not in the format I seek for. The entire scene was to pass date in my controller as current date in a date format in which data in stored in the db (which is of Date data type). – Isha Apr 27 '15 at 07:50
  • so the question is: how to remove the time from `now`? – cfrick Apr 27 '15 at 09:14
  • 1
    This is unnecessarily complicated. First you format a new `Date` object into a `String`, and then you immediately parse it again into a `Date` object. Note that `Date` objects do not have a format. This formatting and parsing does not do anything useful. This code does not do what you think it does. – Jesper Apr 27 '15 at 11:05
  • @Isha First things first, a `DATE` doesn't have any **format**. What you see is for display purpose. For date arithmetic, you should leave the date as it is, there is no need to do all that conversion from date-->string-->date, it is unnecessarily complicated and makes no sense. – Lalit Kumar B Jun 09 '15 at 09:12