Retrieving date from database in jaser - jasper-reports

I have date as datetime in mysql. But when I am retriving value in jasper I am getting null pointer.
net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : new java.text.SimpleDateFormat("YYYY-MM-DD HH:MI:SS").format($F{birthDate})
<field name="birthDate" class="java.util.Date">
<fieldDescription>
<![CDATA[new java.text.SimpleDateFormat("YYYY-MM-DD HH:MI:SS").format($F{birthDate})]]>
</fieldDescription>
</field>
Please help me how do I get the correct date.

According to documentation there are no MI pattern in «Date and Time Patterns». So, if you want to define the minutes then you must use m or mm patern. Also you must use y patern for year, d or dd patern to define the days in month. And use s or ss for seconds, because S is for milliseconds:
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format($F{birthDate})

Related

Why is my formula returning a number instead of a date?

When trying to add days to a date in another column the value returns as a number, not a date.
I have tried to set the column format as the date for both columns B and C. I also tried using the DATEVALUE() function, but I don't think I used it properly.
=ARRAYFORMULA(IF(ROW(B:B)=1,"Second Notification",IF(LEN(B:B), B:B+1,)))
I want the value in column C to return as a date.
use this with TEXT formula:
={"Second Notification";
ARRAYFORMULA(IF(LEN(B2:B), TEXT(B2:B+1, "MM/dd/yyyy hh:mm:ss"), ))}

SparkR - Extracting Month from Date Field

I am using Databricks and SparkR, trying to extract the month from a Date field but keep getting the following error, when trying to using the function month():
Error in as.POSIXlt.default(x, tz = tz(x)) :
Error in as.POSIXlt.default(x, tz = tz(x)) :
do not know how to convert 'x' to class “POSIXlt”
If I use dayofmonth()it runs perfectly so I am not sure what i am doing wrong.
Here is a sample of the fields i am trying to work with.
$ Net_due_date : Date 2017-10-06 2017-10-05 2018-01-17 2017-12-23 2017-08-20 2018-01-17
$ Clearing_Date : Date 2017-10-06 2017-10-17 1900-01-01 2017-12-26 2017-08-24 2018-01-19
Any ideas?
Thanks
I can't be sure without a reproducible example, but try casting the column to timestamp first.
df$Net_due_date_month <- month(cast(df$Net_due_date, 'timestamp'))
You can provide a small subset of your data so that the error is replicated. I suspect that your date column are not date format.
You can try any of these below:
library(lubridate)
df$Net_due_date_month <- month(as.Date(df$Net_due_date, format = '%Y-%m-%d'))
or
df$Net_due_date_month <- month(as.POSIXlt(df$Net_due_date, format = '%Y-%m-%d'))

Is there any way to remove time to compare dates only in jasper?

I have 2 dates.
1st is
<field name="duedate" class="java.util.Date"/>
& second is current date
so I am doing something like this
<![CDATA[ $F{duedate} >= (new java.util.Date()) ? "one" : "two"]]>
which doesn't work if dueDate matches new java.util.Date()
also seems there is some problem with date format. I am using PostgreSQL query to get the date.
Say query is like this
<queryString>select dueDate from table1</queryString>
& then I get that field like this
<field name="duedate" class="java.util.Date"/>
but PostgreSQL dueDate o/p is of type java.sql.date
So field that I declared, will that work as util.date?
java.util.Date has it's own comparison methods ( compareTo(), after(), before() ), and they work in Jasper as well. So you might try something like:
$F{duedate}.compareTo(new java.util.Date() == 0 ? "one" : "two"
Another possible solution i might think of right now is to make the comparison inside the query of your report, like this:
SELECT
duedate as duedate,
CASE WHEN duedate == sysdate THEN "one"
ELSE "two" END as comparison
FROM ....

How to set client specific timezone and date

pardon me if it seems to be a duplicate question.
I have seen many posts already on this topic. However after trying many examples could not find the solution to my problem.
I tried this code
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH )
Date newDate = sdf.parse(sdf.format( new Date( dateTimeString ) ) )
However the second line of code always converts the date to the server specific date and timezone which i don't want. I also tried the following
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz", Locale.ENGLISH )
log.info "+++++++++++++++++hidden date ++++++++ " + params.hiddenGameDateTime.substring(35, 38)
log.info "x = " + sdf.format( new Date ( params.hiddenGameDateTime ))
String tzone = params.hiddenGameDateTime.substring(35, 38)
sdf.setTimeZone( TimeZone.getTimeZone( tzone ) )
log.info "Timezone = " + sdf.getTimeZone().getDisplayName()
Please note that
sdf.format( new Date( dateTimeString ) )
gives me the desired result, however it gives me the string value of the date and the actual value to be stored in database is of the Data type date which can't hold the string value.
the value for date and time in my case gets converted to PST date and time. how can i avoid this. The user input date with timezone should be stored in the database as it is with no change in date and timezone.
An observation: The constructor new Date( dateTimeString ) is deprecated. A better replacement would be something like that:
SimpleDateFormat sdfOriginal = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date d = sdfOriginal.parse(dateTimeString);
Furthermore: An expression like sdf.parse(sdf.format(...)) using the same format object does not make much sense.
But most important, your statement "the second line of code always converts the date to the server specific date and timezone" seems to be based on test output like:
System.out.println(newDate);
This implicitly uses toString() which is based on jvm default time zone, in your case the server time zone. But keep in mind, the internal state of j.u.Date does not reference any time zone. A Date is just a container for a long, namely the seconds since 1970-01-01T00:00:00Z in UTC time zone, that is a global time.
Additional remark:
If you need the client time zone (in a scenario with multiple users in different time zones) to create user-specific formatted date strings, then you indeed need to store the time zone preference of every user in the database, so you can use this information for output in an expression like:
SimpleDateFormat sdf = new SimpleDateFormat("{pattern}";
sdf.setTimeZone(TimeZone.getTimeZone("{user-preference-time-zone}");
String userOutput = sdf.format(date);
Date is always jvm timezone specific. You need to normalize it to standard time and store it in DB to cater with different timezone servers.

What type should you store the date in a database?

Currently I'm storing it as a String, but got problems using it when it comes to querying by date with GQL.
Date date = Calendar.getInstance().getTime();
DateFormat formatter = new SimpleDateFormat("hh:mm:ss, z");
String todayDate = formatter.format(date);
The query:
"SELECT FROM SomeTable p WHERE date = 01/01/2011"
Error:
Exception:
org.datanucleus.store.appengine.query.DatastoreQuery$UnsupportedDatastoreFeatureException:
Problem with query : Right side of expression is
composed of unsupported components.
Left:
org.datanucleus.query.expression.Literal,
Op: / , Right:
DyadicExpression{Literal{5} /
Literal{11}}
How can I search by date?
Always as a date - the database should check on the types of data that it stores thus making sure the data is what it says. the information about what type is known on the insert so check it then rather than later when none has any idea of what the correct value should have been.
the error you are getting is probably not due to this but because the date in the query needs to be made a date if types are correct or if a string needs to be as a single-quoted string see GQL Reference
e.g.
"SELECT FROM SomeTable p WHERE date = '01/01/2011'"
but what date is 06/07/2011 ? I think it is today 6th July, others 7th June. So even if you use strings use the ISO format 2011-07-06 A date type will hide this detail making it easier. Note that GQL only uses the ISO form so even if you got the command to have the date in a string it would fail.
but better as
SELECT FROM SomeTable p WHERE date = DATE( 2011, 1, 1)