Problem with String to Date Conversion - java

I am trying to convert date in String format to sql date and based on that query database to get the result.
Date in string format is :2011-08-11 09:16:00.0
So I am converting it to sql date by using the method:
public static convertStringToSqlDate(String dateString){
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date parsedUtilDate = formater.parse(dateString);
java.sql.Date sqlDate= new java.sql.Date(parsedUtilDate.getTime());
return sqlDate;
}
The resulting date is :2011-08-11
but while doing the query I am not getting desired output
The complete code is
def startDate = params. startDate
def endDate = params. endDate
def formattedTripStartDate =Trip.convertStringToSqlDate(startDate);
def formattedTripEndDate =Trip.convertStringToSqlDte(endDate);
def listOfContracts = Rule.findAll("FROM Rule WHERE name LIKE ? AND client_id = ? AND STR_TO_DATE(contract_begins,'%Y-%m-%d')<= ? AND STR_TO_DATE(contract_terminates,'%Y-%m-%d')>= ?",["%"+q_param+"%",clientId,formattedTripStartDate,formattedTripEndDate] )
Where am I going wrong?
In database the contract_begins is stored as :2011-08-23 00:00:00
Contract domain Class is
class Contract extends Rule {
Date contractBegins
Date contractTerminates
int runningDays
Double contractValue
Double estimatedRevenue
Double actualRevenue
static constraints = {
contractBegins(nullable:true)
contractTerminates(nullable:true)
runningDays(nullable:true)
contractValue(nullable:true)
estimatedRevenue(nullable:true)
actualRevenue(nullable:true)
}
}

Date object didn't format by itself, it return only date and time value. you can get the formatted by only Format supported class in string format like SimpleDateFormat, DateFormat etc.

Why do you want to use a findAll query, and not a criteria. Something like this should do it:
def startDate = params.startDate
def endDate = params.endDate
def tripStartDate=Trip.convertStringToSqlDate(startDate);
def eripEndDate=Trip.convertStringToSqlDte(endDate);
def c = Contract.createCriteria()
def results = c.list {
like("name", "%${q_param}%")
eq("client_id", clientId)
ge('contract_begins', tripStartDate)
le('contract_terminates','tripEndDate)
}
It's much cleaner and you don't have to worry about how the SQL looks like!
Take a deep dive into http://grails.org/doc/latest/ref/Domain%20Classes/createCriteria.html and http://grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.4.2 Criteria where you can find much more information.
Also consider making your code even nicer by adding the criteria into the Domain Class named queries: http://grails.org/doc/latest/ref/Domain%20Classes/namedQueries.html

Related

How to build a Spring Query in Mongo DB with a criteria considering two string date range

I have to build a criteria of a find query using a date field which is stored in String format.
The criteria I've written is like this:
Query findQuery = new Query()
findQuery.addCriteria(Criteria.where(mongoField).gt(startDate).lt(endDate));
where the "mongofield" is a String type, but "startDate" and "endDate" are a LocalDate type.
This criteria obviously doesn't work because the comparison is ineffective.
I've found that I should use $expr operator together with $dateFromString, but I didn't find any clear example of use in Java code.
Please, can anyone help me in writing a good statement in Spring Java code?
You can convert LocalDate to a string and then compare
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(20);
// Define time format converter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
// Convert LocalDate type to String type
Criteria criteria = Criteria.where("mongoField").is("").gte(startDate.format(formatter)).lt(endDate.format(formatter));
Query query = new Query(criteria);

Convert String to "yyy-MM-dd" format with Date as data type

I'm trying to get the data stored in database by getting the date then populate the table.
List<String> contents = new ArrayList<>();
List<Record> records
try {
Session session = sessionFactory.getCurrentSession();
Query<World> query = null;
query = session.createQuery("from World where date like :dateCont, World.class);
query.setParameter("dateCont", "%" + contents.get(0) + "%");
worlds = query.getResultList();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The problem here is that it gives me an error exception:
java.lang.ClassCastException: class java.lang.String cannot be cast to
class java.util.Date (java.lang.String and java.util.Date are in
module java.base of loader 'bootstrap')
I know what's wrong because the List<String> contents values are string and needed to be converted to Date but I tried so many codes and it doesn't work.
//The following are the codes that I tried but it won't work:
//FIRST
Date date = new SimpleDateFormat("MM/dd/yyyy").parse(contentsStart.get(0));
new SimpleDateFormat("yyyy-MM-dd").format(date);
//---------------------------------------
//SECOND
Date newDate;
DateFormat formatter = null;
formatter = new SimpleDateFormat("yyyy-MM-dd");
newDate = (Date) formatter.parse(contentsStart.get(0));
So, is there any way to change the given value to date but it should retains the format "yyyy-MM-dd" and the datatype should be Date.
PS: the format of date in database is "yyyy-MM-dd" also.
Both my date entity field and date from DB is both Date as their
datatype.
Try this:
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date d1= (Date) format.parse(yourDate);
You can use like operator only on string (VARCHAR) fields. In the WHERE clause you have to convert your field to string (using format function) in order to be able to use LIKE. So you have to call the convert function (also) IN THE QUERY.
Unfortunately, there are no DATE-functions in EJBQL, so you will have to switch to the native query. For example, for Oracle you can use TO_CHAR like this
SELECT ... WHERE TO_CHAR(date, 'MM/DD/YYYY') LIKE ...
For good performance you will have to add a functional index.
See also http://www.sqlines.com/oracle-to-sql-server/to_char_datetime
One alternative would be to add a new string column date_string, that will contain the formatted representation of your date and use this column with LIKE. But you will have to make absolutely sure, that both dates are always synchronized.

Converting timestamp from parse.com in java

I'm getting my object's createdAt timestamp back from parse.com as 2014-08-01T01:17:56.751Z. I have a class that converts it to relative time.
public static String timeAgo(String time){
PrettyTime mPtime = new PrettyTime();
long timeAgo = timeStringtoMilis(time);
return mPtime.format( new Date( timeAgo ) );
}
public static long timeStringtoMilis(String time) {
long milis = 0;
try {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sd.parse(time);
milis = date.getTime();
} catch (Exception e) {
e.printStackTrace();
}
return milis;
}
The problem is that this parses the date wrongly. Right now the result says 4 decades ago and this very wrong. What I'm I doing wrong?
Your current date format "yyyy-MM-dd HH:mm:ss" does not work for the given example 2014-08-01T01:17:56.751Z. The format is missing the characters T and Z and the milliseconds.
Change it to:
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
to fix it.
Also check the examples in the JavaDoc of SimpleDateFormat, because it also shows the correct date format for your example: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html.
Expanding #Tom's answer:
The problem
When hardcoding 'Z', you assume that all dates were saved as UTC - which doesn't necessarily have to be the case.
The problem is that SimpleDateFormat does not recognize the literal 'Z'as an alias for UTC's '-0000' offset (For whatever reason, since it claims to be ISO-8601 compliant).
So you can't do
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
since this wrongly assumes all dates will always be written as in UTC, but you can't do
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
either, since this would not be able to parse the date when the literal 'Z' occurs.
Solution 1: Use javax.xml.bind.DatatypeConverter
This datatype converter actually is ISO8601 compliant and can be used as easy as
import javax.xml.bind.DatatypeConverter;
public Long isoToMillis(String dateString){
Calendar calendar = DatatypeConverter.parseDateTime(dateString);
return calendar.getTime().getTime();
}
If you use JAXB anyway, that would be the way to go.
Solution 2: Use conditional formats
final static String ZULUFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
final static String OFFSETFORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
/* This is a utility method, so you want the calling method
* to be informed that something is wrong with the input format
*/
public static Long isoToMillis(String dateString) throws ParseException{
/* It is the default, so we should use it by default */
String formatString = ZULUFORMAT;
if(! dateString.endsWith("Z") ) {
formatString = OFFSETFORMAT;
}
SimpleDateFormat sd = new SimpleDateFormat(formatString);
return sd.parse(dateString).getTime();
}
If you don't already use JAXB, you might want to put this method into a utility class.

Oracle function returns date but no time when called via JPQL

I have an Oracle function which accepts amongst other things, a date as parameter, and returns a date. The function is like this:
FUNCTION my_func(par1 IN DATE, par2 IN NUMERIC) RETURN DATE AS
ret_val DATE;
BEGIN
....
END;
I need to call this function in my Java application, and am currently doing so with JPQL. The code is for a web application running on JBoss 5, and looks like this:
Query q = entityMng.createNativeQuery("SELECT MY_FUNC(?1,?2) FROM DUAL");
java.util.Date now = new Date();
long param2 = 110L;
q.setParameter(1, now);
q.setParameter(2, param2);
java.sql.Date retSql = null;
Object obj = q.getSingleResult();
if (obj != null) {
retSql = (java.sql.Date) obj;
}
After executing the code the retSql variable contains the correct date but has a time = 00.00.000. I obtain the same behaviour even when using java.util.Date as opposed to java.sql.Date, as the type for retSql.
While investigating this problem I cam across this SO post How to convert correctly an Oracle Date field into java.util.Date through JPA, but I don't understand how to use "addScalar" in my case.
Any help would be appreciated, thanks in advance.
Try returning a TIMESTAMP in your function, not a DATE. DATE is now only a date in Oracle, and TIMESTAMP is suppose to be used for date/time. There is a backward compatibility flag you can set in JDBC to control this.
What JPA provider are you using? EclipseLink should be handling this automatically.
What about using XMLAdapter;
public class DateAdapter extends XmlAdapter {
// the desired format
private String pattern = "yyyy-MM-dd HH:mm:ss";
/*few more formats http://www.xyzws.com/javafaq/how-to-use-simpledateformat-class-formating-parsing-date-and-time/142 */
public String marshal(Date date) throws Exception {
return new SimpleDateFormat(pattern).format(date);
}
public Date unmarshal(String dateString) throws Exception {
return new SimpleDateFormat(pattern).parse(dateString);
}
And then adding it to your Entity; for example
private java.util.Date originDate;
#XmlJavaTypeAdapter(DateAdapter.class)
#Column(name = "ORIGIN_DATE", nullable = true)
#XmlElement(namespace = "my-namespace")
public Date getOriginDate() {
return this.originDate;
}
Here is a tutorial that I used to learned about this;
http://weblogs.java.net/blog/kohsuke/archive/2005/04/xmladapter_in_j.html

different date format in java after fetching from oracle

In my db max date is as : 27-FEB-12
when i am fetching data by java from db that is:
select to_char(max(CREATE_DT),'dd-mm-yyyy') from PROFILE_DETAILS;
gives me 2012-02-27 00:00:00.0
How can i convert it to: 27-FEB-12( i am trying to use indian date format)
Any idea please
I don't know why you need to_char function in your query. If you are fetching data by jdbc, oracle could give you Date object. It is in your case much easier to convert into different format (String) in future.
anyway based on your current requirement, with to_char, you get a String 2012-02-27 00:00:00.0. now you want to get another string 27-FEB-12. you could do something like below(exception handling was omitted):
final String s = "2012-02-27 00:00:00.0";
String newDateString = new SimpleDateFormat("dd-MMM-yy").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(s));
this will give you 27-Feb-12
String strDate = "2012-02-27 00:00:00.0";
String TimeZoneIds = TimeZone.getDefault().getID();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MMM-yyyy");
sdf2.setTimeZone(TimeZone.getTimeZone(TimeZoneIds));
try {
Date date = sdf1.parse(strDate);
String strFinalDate = sdf2.format(date);
System.out.println(strFinalDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In an Oracle DATE column there is no format; the string representation you see when you select max(create_dt)) from profile_details in SQL*Plus, say, is using an implicit format mask from your NLS settings, which appears to be DD-MON-RR in that client.
You JDBC call is applying an explicit format mask, which is the right thing to do if you want Java to treat it as a String, not least because it may have different NLS settings. But your mask doesn't match what you say you want; you're specifying DD-MM-YYYY when you want DD-MON-RR.
But it also looks like you're probably retrieving the value from the JDBC call with a getDate() call, and it's being implicitly cast back to a Java Date object type. If you want to treat it as a Date in Java, then you don't need the to_char in your select, and you need to use Java tools (e.g. SimpleDateFormat as #Andrew Logvinov suggests) to turn it into a String as needed. If you're only ever treating it as a String - for immediate display, say - then use getString() instead, and fix your date format mask in the query.
Edit
If you retrieve the value from JDBC with getDate() and want to see the value as a String in the format you specified, you need to do something like:
Date raw_date;
String string_date;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy");
raw_date = <resultSet>.getDate(1);
string_date = sdf.format(raw_date);
select to_char(max(CREATE_DT),'dd-MON-yy') from PROFILE_DETAILS;

Categories