Java DynamicReports format dates regarding to locale - java

I'm using DynamicReports API for building reports.
I'm setting the Locale of the report and format the Date columns of the report, but the Dates are always formatted like 10/12/2009 10:54:44 AM no matter what the Locale is.
The code looks like:
rep.setTemplate(Templates.reportTemplate.setLocale(res.getLocale()));
...
if (rs.getString(i).contains("00:00:00"))
rep.addColumn(col.column(title, name, type.dateType()));
else
rep.addColumn(col.column(title, name, type.dateYearToSecondType()));
Is there a way to automatically format dates regarding to the Locale of the report or have I to use a custom ValueFormatter?

I also tryed the Parameter Map with no success
JasperReportBuilder rep = report()
.setDataSource(query, conn.getConnection())
.setParameter(JRParameter.REPORT_LOCALE, res.getLocale());
.setTemplate(Templates.reportTemplate.setLocale(res.getLocale()));
There is no way to automatically format the dates.
The only way is to use patterns for the date column in respect to the locale.
TextColumnBuilder<Date> column = col.column(title, name, type.dateType());
if (res.getLocale().equals("EN") {
column.setPattern("dd.MM.yyyy");
}
else if (res.getLocale().equals("US") {
column.setPattern("MM/dd/yyyy");
}
else {
...
}
rep.addColumn(column);

I managed to format the date field in dynamic jasper in this way.
ColumnBuilder time = ColumnBuilder.getNew();
time.setTitle("Login Time");
time.setWidth(200);
time.setColumnProperty("emp.logintime", Date.class.getName()).setPattern("dd/MM/yyyy hh:mm:ss a");
drb.addColumn(time.build());

Related

Create ISO 8601 date String with GWT

Here's my code:
public static String getStringFormat(Date inputDate, String timeZone){
String strFormat = null;
try{
final TimeZone computedTimeZone = TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(timeZone));
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.ISO_8601);
strFormat = dateTimeFormat.format(inputDate, computedTimeZone);
Date d = new Date(strFormat);
strFormat = dateTimeFormat.format(d, TimeZone.createTimeZone(0));
String[] s = strFormat.split("\\+");
strFormat = s[0];
}catch(Exception e){
Console.log(e.getMessage());
}
return strFormat;
}
For input, new Date() and Etc/GMT+3 this function returns null. What could be wrong?
Error
Error: NullPointerException: undefined
at NBF_g$.QBF_g$ [as createError_0_g$] (NullPointerException.java:40)
at NBF_g$.ub_g$ [as initializeBackingError_0_g$] (Throwable.java:113)
at NBF_g$.bb_g$ (Throwable.java:61)
at NBF_g$.Ib_g$ (Exception.java:25)
at NBF_g$.avq_g$ (RuntimeException.java:25)
at NBF_g$.gfs_g$ (JsException.java:34)
at new NBF_g$ (NullPointerException.java:27)
at new wou_g$ (JSONString.java:43)
The method TimeZoneInfo.buildTimeZoneData(String tzJSON) doesn't accept the name of the zone, but needs a JSON string full of the details of how that zone works. It turns out that the browser doesn't come to you with all of the details of how all time zones work, so your app has to already be prepared to handle them.
GWT ships with all of the timezones (though they are currently a little out of date, and should be updated in this next release), but you have to tell the compiler which ones you want, or it will compile them out. The full list of all possible timezones and their offsets, etc is not small, so I would encourage you to limit the list.
These are stored in the constants interface TimeZoneConstants. Here is how you might use it:
TimeZoneConstants constants = GWT.create(TimeZoneConstants.class);
// This is the shorthand for TimeZone.createTimeZone(TimeZoneInfo.buildTimeZoneData(...))
TimeZone computedTimeZone = TimeZone.createTimeZone(constants.americaAdak());
//...
If you want to use the timezone string instead, say, passed from the server, you could build a map of the possible timezones that are supported. Be aware though that the full map is very large (200KB just for the timezones in the "America/..." group).
computedTimeZone = TimeZone.createTimeZone(constants.americaAdak());
zones.put(computedTimeZone.getID(), computedTimeZone);
computedTimeZone = TimeZone.createTimeZone(constants.americaAnchorage());
zones.put(computedTimeZone.getID(), computedTimeZone);
computedTimeZone = TimeZone.createTimeZone(constants.americaAnguilla());
zones.put(computedTimeZone.getID(), computedTimeZone);
//...
Then you can read out a specific item from the map as needed:
String tzName = Window.prompt("Enter a timezone name", "America/Chicago");
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.ISO_8601);
String strFormat = dateTimeFormat.format(inputDate, zones.get(tzName));
//...
In your comment, you clarified the question, that you only need to deal with offsets, not the full TimeZone string format, i.e. Etc/GMT+3, meaning "Offset of +3 hours from GMT". This is easier to handle - simply parse out the +3 into a number, and use the TimeZone.createTimeZone(int timeZoneOffsetInMinutes) method. This will not understand daylight savings time, but that wouldn't be possible without the full name of the timezone or list of offsets, etc (which gets to why that JSON is so large).
//TODO, implement parse(), don't forget about negative numbers
int offsetInHours = parse(timeZone);
TimeZone computedTimeZone = TimeZone.createTimeZone(60 * offsetInHours);
//...

DBunit - Unable to typecast <1997/02/14> to TimeStamp

I'm doing an integration testing with DBUnit (2.49) + Hibernate (4.1.3) following this tutorial.
Production database : Oracle 10
Test database : Hsqldb 2.3.3
Context
My data contains the current format of date : yyyy/MM/dd. However,according to DBUnit faq, DBUnit only supports this format yyyy-mm-dd hh:mm:ss.fffffffff, so I had to create a new format for TimeStamp.
How I tried to fix it
I created a CustomTimeStampDataType based on this tutorial. I changed this part:
String formats[] = {"yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm a", "yyyy-MM-dd HH:mm:ss.fffffffff"};
into this one:
String formats[] = {"yyyy/MM/dd"};
I created a CustomeDataTypeFactory following the same tutorial. I only make it extend Oracle10DataTypeFactory rather than DefaultDatatTypeFactory.
In HibernateDBUnitTestCase, I override setDatabaseConfig() with the following:
#Override
protected void setUpDatabaseConfig(DatabaseConfig config){
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new CustomDataTypeFactory());
}
But I got new errors
I ran a unit test and got this error.
org.dbunit.dataset.datatype.TypeCastException: Unable to typecast value <1997/02/14> of type <java.lang.String> to TIMESTAMP
at org.dbunit.dataset.datatype.TimestampDataType.typeCast(TimestampDataType.java:120)
at org.dbunit.dataset.datatype.TimestampDataType.setSqlValue(TimestampDataType.java:176)
at org.dbunit.database.statement.SimplePreparedStatement.addValue(SimplePreparedStatement.java:73)
at org.dbunit.operation.RefreshOperation$RowOperation.execute(RefreshOperation.java:189)
at org.dbunit.operation.RefreshOperation.execute(RefreshOperation.java:113)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:156)
at test.HibernateDbUnitTestCase.setUp(HibernateDbUnitTestCase.java:85)
at test.PlayerTest.setUp(PlayerTest.java:117)
Caused by: java.lang.IllegalArgumentException: Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
at java.sql.Timestamp.valueOf(Unknown Source)
at org.dbunit.dataset.datatype.TimestampDataType.typeCast(TimestampDataType.java:116)
... 20 more
That was weird, it seemed like my CustomTimeStamp was not called, so I changed the date in the dataset using the default format : 1997-02-14 00:00:00.0, and ran the unit test again. Then I got:
org.dbunit.dataset.datatype.TypeCastException: Unable to typecast value <1997-02-14 00:00:00.0> of type <java.lang.String> to TIMESTAMP
at test.CustomTimestampDataType.typeCast(CustomTimestampDataType.java:69)
at test.CustomTimestampDataType.setSqlValue(CustomTimestampDataType.java:84)
at org.dbunit.database.statement.SimplePreparedStatement.addValue(SimplePreparedStatement.java:73)
at org.dbunit.operation.RefreshOperation$RowOperation.execute(RefreshOperation.java:189)
at org.dbunit.operation.RefreshOperation.execute(RefreshOperation.java:113)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:156)
at test.HibernateDbUnitTestCase.setUp(HibernateDbUnitTestCase.java:85)
at test.PlayerTest.setUp(PlayerTest.java:117)
That means CustomTimeStamp was actually called. Seems like, the problem stemed from DatabaseTestCase.setUp which somehow called the wrong TimeStampDataType.
How could I fix this issue?
My first option was to replace every yyyy/MM/dd into yyyy-mm-dd in the dataset using regular expressions. This worked fine, until I had to test a method that selected a date based on a request (so the format is yyyy-mm-dd) and compared it to the current date. ( so the format is yyyy / mm / dd). Hsqldb can't compare two dates with different format.
My second option was to decompile dbunit.jar, rewrite TimeStampDataType based on the tutorial. I'm unfamiliar with bytecode writing so before entering uncharted waters, I wanted to know if you had another solution.
Thank you in advance
Fixed it!
So I ended up using my second option.
This is the detailed path for those who need it.
Download dbUnit.2.2.source.jar
Unzip the jar
Go to Eclipse, File > New > Java Project
Uncheck "Use default location"
In Location : specify the path to the new folder created from the jar
Click on Finish
Modify the TimestampDataType.java (if needed)
Instead of ts = java.sql.Timestamp.valueOf(stringValue); use the code below
String formats[] =
{"dd/MM/yyyy HH:mm:ss.SS"}; //and more depending on your need
Timestamp ts = null;
for (int i = 0; i < formats.length; i++)
{
SimpleDateFormat sdf = new SimpleDateFormat(formats[i]);
try {
java.util.Date date = sdf.parse(stringValue);
ts = new Timestamp(date.getTime());
return ts;
}
catch( ParseException e) {
}
Modify the DateDataType.java (if needed)
Instead of return java.sql.Date.valueOf(stringValue); , use the code below
String formats[] =
{"dd/MM/yyyy"}; //and more depending on your need
for (int i = 0; i < formats.length; i++)
{
SimpleDateFormat sdf = new SimpleDateFormat(formats[i]);
try {
java.util.Date date = sdf.parse(stringValue);
java.sql.Date datesql = new java.sql.Date(date.getTime());
return datesql;
}
catch( ParseException e) {
}
}
Right-click on your project, then Export
Select JAR file, then Next
Fill the export destination then Finish.
You just have to add this new jar to the library to make it work.

Locales with ISO calendar only

I have a java program where the user can choose the locale which he wants from a checkbox. in order to populate this checkbox I use the Calendar.getAvailableLocales() method. The problem is that this method returns locales which use dates which are not Gregorian and this causes problems in my program which only supports Gregorian dates (ISO). Two examples are the Japanese (Japan, jP) locale and the Thai (Thailand, TH) locale. I do not want these types of locales to appear in the program. How can I filter the list such that only locales with ISO calendar formats are available in the checkbox? Thanks.
EDIT:
Ok. Here is the code for the JLocaleChooser combo box that I use:
locales = Calendar.getAvailableLocales();
localeCount = locales.length;
for (int i = 0; i < localeCount; i++) {
if (locales[i].getCountry().length() > 0) {
addItem(locales[i].getDisplayName());
}
}
After that I set the locale of the system based on the user choice as such:
myLocale2 = theJLocaleChooser.getLocale();
Locale.setDefault(myLocale2);
If the user chooses the Japanese (Japan) locale then everything is fine. if he/she chooses the Japanese (Japan, JP) locale my program does not perform operations like it should. For example, somewhere in the program I get the current month and use the information in a sql statement as such:
Calendar cal0 = Calendar.getInstance();
int month1 = cal0.get(Calendar.MONTH);
month1++; //since 0 is january
int year1 = cal0.get(Calendar.YEAR);
connect.query = "SELECT name1, name2 FROM table1 WHERE MONTH(date) = " + month1 + " AND YEAR(date) = " + year1;
This sql query returns an empty dataset if the user chose Japanese(Japan, JP) but returns the correct dataset if the user chose the Japanese(Japan) locale. The reason is that the value of the year1 variable if I use the Japanese (Japan, JP) locale is 26 (I used a println command) but it is 2014 (like it should be) if I use other locales including the Japanese (Japan) one. Therefore I need locales that use the "regular" years (2012, 2013,...) not other years (like the Islamic Hijra for example).
You can just check whether Calendar.getInstance(Locale) returns a GregorianCalendar:
for (Locale locale : Calendar.getAvailableLocales()) {
if (locale.getCountry().length() > 0
&& Calendar.getInstance(locale) instanceof GregorianCalendar) {
addItem(locale.getDisplayName());
}
}

How to create Currency instance with non ISO 3166 country like en_UK?

In my app, I get the user's default locale using Locale.getDefault() and then pass that to Currency.getInstance(Locale). It mostly works, but I have started getting reports from users which show the following IllegalArgumentException in the stack trace:
Caused by: java.lang.IllegalArgumentException: Unsupported ISO 3166
country: en_UK at java.util.Currency.getInstance(Currency.java:81) at
org.
I expected Android to only return valid locales, but that is apparently not the case.
How do I handle such cases to make sure I only get valid ISO 3166 locales? The easy way will be to handle this special case, but I would rather use a generic solution if there is one.
Anyone have experience with this? Thanks.
The ISO 3166 two-letter abbreviation for the UK is not UK, the correct id is GB. UK is there for compatibility reasons (a mistake made in the past).
I did look for other exeptions but did not find any, so for now i would just handle the special case.
Locale loc = new Locale("en","UK"); // test code
if(loc.getCountry().equals("UK")){
loc = new Locale(loc.getLanguage(), "GB");
}
Currency cur = Currency.getInstance(loc);
Currency.getInstance(...) expects a locale of the form "en_US", however, Locale.getDefault() does not always return a locale of this form.
To prevent crashes in your app, you can use something like this:
public static String getCurrencySymbol(){
String cursym;
try {
cursym = Currency.getInstance(Locale.getDefault()).getSymbol();
} catch (IllegalArgumentException e) {
cursym = "?"; // default symbol
}
return cursym;
}
You can try to figure out a better way to get the most appropriate symbol, or just let the user choose one.
Your users (with dev settings enabled) or testers have manually set a weird and invalid country. This bug occurred to us, but it turned out our testers had configured Appium to set the device locale to en-UK :).
A regular user can not select en-UK or any other invalid locale.
String symbolLocale ="";
int localeLength = mLocale.split("-").length;
if (localeLength == 2) {
symbolLocale = new Locale(
mLocale.split("-", 2)[0],
mLocale.split("-", 2)[1]
);
} else if (localeLength == 3) {
symbolLocale = new Locale(
mLocale.split("-", 3)[0] + "-" +
mLocale.split("-", 3)[1],
mLocale.split("-", 3)[2]
);
}
symbolString = Currency.getInstance(symbolLocale).getSymbol(symbolLocale);

how to map java.util.Date to MySql Date in Ibatis when I take input through JSON

I am taking date of birth as input using JSON
{"dateOfBirth":"1973-08-26"}
This field exists in Person.java class
import java.util.Date;
public class Person {
Date dateOfBirth;
//Some other fields
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
}
This is mapped to person table in mysql database.
I am querying the database like:
entityId = (Long) session.selectOne("ValidatePerson", registerUserRequestParams);
Following are the entries I am making in my mapper.xml
<select id="ValidatePerson" parameterMap="ValidatePersonMap" resultType="long">
select person.entityId
from person
where
//Some other Validation checks
<if test="dateOfBirth != null">
and person.dateOfBirth = #{dateOfBirth}
</if>
);
</select>
I have a prameter Map as
<parameterMap id="ValidatePersonMap" type="java.util.HashMap">
<parameter property="dateOfBirth" javaType="java.util.Date" jdbcType="DATE" mode="IN"/>
</parameterMap>
I am not able to get any result from database.It does not select any row even though value exists.I have checked that none of other validation checks are failing. If I pass dateOfBirth as null in JSON then then I get result.
I have also written a test case and setting request as follows:
Date dob = new Date(73,7,26);
request.setDateOfBirth(dob);
When I pass values from test case as mentioned above I get result from database.
Problem occurs only when i get request parameters using json.
The format of JSOn and the format stored in DB are same
One work around I have is to manually convert java.util.Date to String in above format and pass it as string. But this is pretty bad approach and client would not like it.
Use Timestamp. Consider Joda time plug. ...and read this answer.
These three will absolutely do the magic.
Good luck!
Have you tried formatting the java date. Maybe the formats are different so it can't match between the JSON and the one stored in your db?
This worked for me:
mysql attribute type is Timestamp
and I format the date for JSON in Java like this:
Date d = new Date();
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
String sd = sdf.format(d);
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("HH:MM:ss");
String fakeit =sd+"T"+sdf2.format(d);
I then use fakeit

Categories

Resources